PHP code example of vladimircatrici / shopify-api

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

    

vladimircatrici / shopify-api example snippets



use VladimirCatrici\Shopify;
Shopify\ClientManager::setConfig('default', [
    'domain' => 'your-shop-handle',
    'access_token' => 'your-access-token',
]);
$api = Shopify\ClientManager::get('default');

$api->get('orders');

$numProducts = $api->get('products/count'); // int
$products = $api->get('products'); // array
foreach ($products as $product) {
  echo sprintf('#%d. %s<br>', 
    $product['id'], $product['title']);
}

$products = $api->get('products', [
  'fields' => 'id,title',
  'limit' => 250,
  'page' => 2
]);

$product = $api->get('products/123456789');
echo sprintf('#%d. %s', $product['id'], $product['title']);

$product = $api->put('products/123456789', [
    'title' => 'New title'
]);
echo sprintf('#%d. %s', $product['id'], $product['title']); // #1. New title

$product = $api->post('products', [
    'title' => 'New product' 
]);

$api->delete('products/123456789');
if ($api->respCode == 200) {
    // Item has been successfully removed
}

use VladimirCatrici\Shopify\ClientManager;
$api = ClientManager::get('default');
$products = new Collection($api, 'products');
foreach ($products as $product) {
    printf('#%d. %s [$%f], 
        $product['id'], $product['title'], $product['price']
    );
}

use VladimirCatrici\Shopify;
if (Shopify\Webhook::validate('your-webhook-token')) {
    printf('`%s` webhook triggered on your store (%s). Data received: %s', 
        Shopify\Webhook::getTopic(), 
        Shopify\Webhook::getShopDomain(),
        Shopify\Webhook::getData()
    );
} else {
    // Invalid request | Unauthorized webhook | Data corrupted
}

// You can also get webhook data as array right away
$data = Shopify\Webhook::getDataAsArray();
printf('Product ID#%d, product title: %s', 
    $data['id'], $data['title']
);

use VladimirCatrici\Shopify\Exception\RequestException;
try {
    $products = $api->get('products');
} catch (RequestException $e) {
    $request = $e->getRequest(); // PSR-7/Request object

    $response = $e->getResponse(); // PSR-7/Response object
    $code = $response->getStatusCode(); // int
    $headers = $response->getHeaders(); // array
    $bodyStream = $response->getBody(); // Stream (PSR-7/StreamInterface)
    $bodyContent = (string) $response->getBody(); // string (or $body->__toString())
    
    // Details of the errors including exception message, request and response details
    echo $e->getDetailsJson();
}