PHP code example of randock / vignette-api-client

1. Go to this page and download the library: Download randock/vignette-api-client 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/ */

    

randock / vignette-api-client example snippets



use Softonic\GraphQL\Client;
use Softonic\GraphQL\ResponseBuilder;
use Randock\Vignette\Api\Client\ApiClient;
use Randock\Vignette\Api\Util\ClientBuilder;
use Randock\Vignette\Api\Client\Object\Enum\OrderStatus;
use Randock\Vignette\Api\Client\Object\Input\ListOrdersInput;

// This example uses "null" as persistence strategy. DO NOT DO THIS IN PROD.
$client = new Client(
    ClientBuilder::newClient(
        'ENDPOINT',
        'JWT_ENDPOINT',
        [
            'username' => 'XXX',
            'password' => 'XXX'
        ],
        null
    ),
    new ResponseBuilder()
);

// The API is strongly typed, so all responses are easy to use.
// You need to select all fields that you are going to use in the response.
// The API uses GRAPHQL underneath, so not selecting a field means it won't be available.
// See the docs for all possible fields.
$apiClient = new ApiClient($client);
$ordersResponse = $apiClient->orders(
    [
        'paging' => ['total'],
        'orders' => ['id', 'status']
    ],
    new ListOrdersInput(1, 10, null, null)
);

// The total number of orders can be found in the paging section.
echo sprintf(
    "Found %d orders\n\n",
    $ordersResponse->getPaging()->getTotal()
);

// Loop through the orders and print some data.
foreach ($ordersResponse->getOrders() as $order) {
    echo sprintf(
        "Status of order %s is %s. Order is completed: %d\n",
        $order->getId(),
        $order->getStatus(),
        $order->getStatus() === OrderStatus::COMPLETED
    );
}

// If you try to access a field that you didn't request, an exception is thrown.
// FieldNotSelectedException: The field orderId has not been selected in the query. Add it to the query if you need it.
echo $ordersResponse->getOrders()[0]->getOrderId();