PHP code example of robwittman / shopify-php-sdk

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

    

robwittman / shopify-php-sdk example snippets


$client = new Shopify\Api(array(
    'api_key' => '<api_key>',
    'api_secret' => '<api_secret>',
    'myshopify_domain' => 'store.myshopify.com',
    'access_token' => '<store_access_token>'
));

$client = new Shopify\PrivateApi(array(
    'api_key' => '<api-key>',
    'password' => '<password>',
    'shared_secret' => '<shared-secret>',
    'myshopify_domain' => '<store>.myshopify.com'
));

$service = new Shopify\Service\ProductService($client);
$service->all(); #Fetch all products, with optional params
$service->get($productId); # Get a single product
$service->count(); # Count the resources

$service = new Shopify\Service\ProductService($client);
$product = new Shopify\Object\Product();
# Set some product fields
$product->title = 'Test Product';
$product->vendor = 'Printer';

$service->create($product);

$service = new Shopify\Service\ProductService($client);
$product = $service->get($productId);
# Set some product fields
$product->title = 'Test Product';
$product->vendor = 'Printer';

$service->update($product);

$service = new Shopify\Service\ProductService($client);
$service->delete($productId);

$service = new Shopify\Service\GraphQLService($client);
$service->graph(
  '{
    products(query: "created_at:<2019", first: 5) {
      edges {
        node {
          title
          description
        }
      }
    }
  }'
);

$service = new Shopify\Service\GraphQLService($client);
$service->graph(
  'mutation productCreate($input: ProductInput!){
    productCreate(input: $input) {
      product {
        id
      }
    }
  }',
  ['input' => ['title' => 'Sweet new product','productType' => 'Snowboard','vendor' => 'JadedPixel']]
);

$client = new Shopify\Api($params);
$helper = $client->getOAuthHelper();

$redirectUri = 'https://localhost/install.php';
$scopes = 'write_products,read_orders,...';

$authorizationUrl = $helper->getAuthorizationUrl($redirectUri, $scopes);
header("Location: {$authorizationUrl}");

$client = new Shopify\Api($params);
$helper = $client->getOAuthHelper();

$token = $helper->getAccessToken($code);
echo $token->access_token;
echo $token->scopes;

use Shopify\Enum\Fields\ProductFields;
use Shopify\Enum\Fields\ProductVariantFields;

$product = $service->get($productId);
echo $product->created_at->format('Y-m-d H:i:s');
echo $product->title;

foreach ($product->variants as $variant) {
    echo $variant->option1;
    echo $variant->option2;
}
shell
composer