PHP code example of webtize / shopify-graphql-sdk

1. Go to this page and download the library: Download webtize/shopify-graphql-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/ */

    

webtize / shopify-graphql-sdk example snippets


use ShopifyGraphQL\ClientFactory;

// Simple setup with auto-discovery
$client = ClientFactory::create(
    'your-store.myshopify.com',  // or just 'your-store'
    'your-access-token-here'
);

// Get shop information
$response = $client->getShopInfo();
if ($response->isSuccessful()) {
    $shop = $response->get('shop');
    echo "Shop: " . $shop['name'];
}

use ShopifyGraphQL\Products;

$products = new Products($client);

// List products
$response = $products->list(10);
foreach ($response->get('products.edges', []) as $edge) {
    echo $edge['node']['title'] . "\n";
}

// Get single product
$product = $products->get('gid://shopify/Product/123');
echo $product->get('product.title');

// Create product
$newProduct = $products->create([
    'title' => 'New Product',
    'productType' => 'Electronics',
    'vendor' => 'ACME Corp'
]);

use ShopifyGraphQL\QueryBuilder;

$query = QueryBuilder::query('GetCustomers')
    ->variable('first', 'Int!', 10)
    ->field('customers', function($builder) {
        $builder->field('edges', function($edge) {
            $edge->field('node', [
                'id', 'email', 'firstName', 'lastName'
            ]);
        });
    });

$response = $client->query($query->build(), ['first' => 5]);

$client = ClientFactory::create(
    'your-store.myshopify.com',
    'your-access-token',
    [
        'timeout' => 30,           // Request timeout in seconds
        'max_retries' => 3,        // Max retry attempts for rate limits
        'headers' => [             // Additional headers
            'X-Custom-Header' => 'value'
        ]
    ]
);

use GuzzleHttp\Client as GuzzleClient;use Http\Adapter\Guzzle7\Client as GuzzleAdapter;use Nyholm\Psr7\Factory\Psr17Factory;use ShopifyGraphQL\ClientFactory;

$httpClient = new GuzzleAdapter(new GuzzleClient([
    'timeout' => 60,
    'verify' => true
]));

$factory = new Psr17Factory();

$client = ClientFactory::createWithHttpClient(
    $httpClient,
    $factory,  // Request factory
    $factory,  // Stream factory  
    'your-store.myshopify.com',
    'your-access-token'
);

use ShopifyGraphQL\AuthenticationException;use ShopifyGraphQL\RateLimitException;use ShopifyGraphQL\ShopifyGraphQLException;

try {
    $response = $client->query($query);

} catch (AuthenticationException $e) {
    echo "Invalid credentials: " . $e->getMessage();

} catch (RateLimitException $e) {
    echo "Rate limited. Retry after: " . $e->getRetryAfter() . " seconds";

} catch (ShopifyGraphQLException $e) {
    echo "GraphQL Error: " . $e->getMessage();

    if ($e->hasGraphqlErrors()) {
        foreach ($e->getGraphqlErrors() as $error) {
            echo "- " . $error['message'] . "\n";
        }
    }
}

$products = new Products($client);
$allProducts = [];
$cursor = null;

do {
    $response = $products->list(50, $cursor);

    if (!$response->isSuccessful()) {
        break;
    }

    $data = $response->get('products');
    $edges = $data['edges'] ?? [];

    foreach ($edges as $edge) {
        $allProducts[] = $edge['node'];
        $cursor = $edge['cursor'];
    }

    $hasNextPage = $data['pageInfo']['hasNextPage'] ?? false;

} while ($hasNextPage);

$query = QueryBuilder::query('GetProductsAndVariants')
    ->variable('productId', 'ID!')
    ->variable('first', 'Int', 10)
    ->field('product', function($builder) {
        $builder->field('id')
                ->field('title')
                ->field('variants', function($variantBuilder) {
                    $variantBuilder->field('edges', [
                        'node' => ['id', 'title', 'price']
                    ]);
                });
    })
    ->build();
bash
# For Guzzle (recommended)
composer equire symfony/http-client php-http/httplug-bundle

# For cURL adapter
composer