PHP code example of dsync / php-sdk

1. Go to this page and download the library: Download dsync/php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dsync / php-sdk example snippets



use Dsync\PhpSdk\Endpoint\RealtimeRequest;
use Dsync\PhpSdk\Exception\RealtimeRequestException;

$data = ['foo' => 'bar'];

// create a new realtime request object with your authorization token, endpoint token and data
$request = new RealtimeRequest('yourAuthToken', 'yourEndpointToken', $data);

try {
    // send a create request
    $result = $request->create();
} catch (RealtimeRequestException $e) {
    // do something if there is an exception
}



use Dsync\PhpSdk\Endpoint\RealtimeRequest;
use Dsync\PhpSdk\Exception\RealtimeRequestException;

$data = ['foo' => 'foo'];

// create a new realtime request object
$request = new RealtimeRequest();

// set your authorization token, endpoint token and data on the request
$request
    ->setAuthToken('yourAuthToken')
    ->setEntityToken('yourEndpointToken')
    ->setData($data);

try {
    // send an update request
    $result = $request->update();
} catch (RealtimeRequestException $e) {
    // do something if there is an exception
}



use Dsync\PhpSdk\Endpoint\RealtimeRequest;
use Dsync\PhpSdk\Exception\RealtimeRequestException;

// create a new realtime request object with your authorization token and endpoint token
$request = new RealtimeRequest('yourAuthToken', 'yourEndpointToken');

// set your primary key for the entity you wish to delete as defined by the datalayout
$request->setEntityId('primaryKeyAsDefinedInDatalayout');

try {
    // send a delete request
    $result = $request->delete();
} catch (RealtimeRequestException $e) {
    // do something if there is an exception
}


use Dsync\PhpSdk\Utils\Generator\Datalayout;
use Dsync\PhpSdk\Utils\Generator\Endpoint;
use Dsync\PhpSdk\Utils\Generator\Field;

// create a new field object
$field = new Field();

// set field information
// a list of field type constants can be found in the Dsync\PhpSdk\Utils\Generator\Field class
$field
    ->setPrimaryKey(true)
    ->setRequired(true)
    ->setTreekey('product.sku')
    ->setDescription('A product SKU')
    ->setName('sku')
    ->setType(Field::TYPE_TEXT);

// create a new endpoint object
$endpoint = new Endpoint();

// set endpoint information and add all fields using the addField method
$endpoint
    ->setEntityName('product')
    ->setTreekey('product')
    ->setEntityToken('source-1-product-b5503a0ae5f3bc01b6a2da68afd33305')
    ->setEndpointUrl('/entity/product')
    ->addField($field);

// finally create a new datalayout object
$datalayout = new Datalayout();

// add all endpoints using the addEndpoint method and call the generate method 
// to create the datalayout array
$datalayoutArray = $datalayout
    ->addEndpoint($endpoint)
    ->generate();

$response = new JsonResponse();

$responseArray = [
    'status' => 200,
    'message' => 'OK',
    'detail' => '',
    'data' => $datalayoutArray
];

$response->setData($responseArray);

return $response;

use Dsync\PhpSdk\Utils\Generator\Endpoint;
use Dsync\PhpSdk\Utils\Generator\EntityToken;

$entityTokenClass = new EntityToken();

// generate the entity token using an endpoint name as reference
$entityToken = $entityTokenClass->generateEntityToken('product');

$endpoint = new Endpoint();

// set the entity token on the endpoint object
$endpoint
    ->setEntityName('product')
    ->setTreekey('product')
    ->setEntityToken($entityToken)
    ->setEndpointUrl('/entity/product')
    ->addField($field);

// save $entityToken to make requests at a later time

composer