1. Go to this page and download the library: Download ohmybrew/basic-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/ */
ohmybrew / basic-shopify-api example snippets
use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
use Gnikyt\BasicShopifyAPI\Options;
use Gnikyt\BasicShopifyAPI\Session;
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session('example.myshopify.com', 'access-token-here'));
// Now run your requests...
$result = $api->rest(...);
use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
use Gnikyt\BasicShopifyAPI\Options;
use Gnikyt\BasicShopifyAPI\Session;
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session('example.myshopify.com', 'access-token-here'));
// Now run your requests...
$promise = $api->restAsync(...);
$promise->then(function (array $result) {
// ...
});
use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
use Gnikyt\BasicShopifyAPI\Options;
use Gnikyt\BasicShopifyAPI\Session;
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session('example.myshopify.com', 'access-token-here'));
// Now run your requests...
$result = $api->graph(...);
use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
use Gnikyt\BasicShopifyAPI\Options;
use Gnikyt\BasicShopifyAPI\Session;
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session('example.myshopify.com', 'access-token-here'));
// Now run your requests...
$promise = $api->graphAsync(...);
$promise->then(function (array $result) {
// ...
});
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
$options->setApiKey(env('SHOPIFY_API_KEY'));
$options->setApiSecret(env('SHOPIFY_API_SECRET'));
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session($_SESSION['shop']));
$code = $_GET['code'];
if (!$code) {
/**
* No code, send user to authorize screen
* Pass your scopes as an array for the first argument
* Pass your redirect URI as the second argument
*/
$redirect = $api->getAuthUrl(env('SHOPIFY_API_SCOPES'), env('SHOPIFY_API_REDIRECT_URI'));
header("Location: {$redirect}");
exit;
} else {
// We now have a code, lets grab the access token
$api->requestAndSetAccess($code);
// You can now make API calls
$request = $api->rest('GET', '/admin/shop.json'); // or GraphQL
}
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
$options->setApiKey(env('SHOPIFY_API_KEY'));
$options->setApiSecret(env('SHOPIFY_API_SECRET'));
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session($_SESSION['shop']));
$code = $_GET['code'];
if (!$code) {
/**
* No code, send user to authorize screen
* Pass your scopes as an array for the first argument
* Pass your redirect URI as the second argument
* Pass your grant mode as the third argument
*/
$redirect = $api->getAuthUrl(env('SHOPIFY_API_SCOPES'), env('SHOPIFY_API_REDIRECT_URI'), 'per-user');
header("Location: {$redirect}");
exit;
} else {
// We now have a code, lets grab the access object
$api->requestAndSetAccess($code);
// You can now make API calls
$request = $api->rest('GET', '/admin/shop.json'); // or GraphQL
}
// Will return true or false if HMAC signature is good.
$valid = $api->verifyRequest($_GET);
// Create options for the API
$options = new Options();
$options->setType(true); // Makes it private
$options->setVersion('2020-01');
$options->setApiKey(env('SHOPIFY_API_KEY'));
$options->setApiPassword(env('SHOPIFY_API_PASSWORD'));
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session($_SESSION['shop']));
// Now run your requests...
$result = $api->rest(...);
// Create options for the API
$options = new Options();
$options->setType(true); // Makes it private
$options->setVersion('2020-01');
$options->setApiPassword(env('SHOPIFY_API_PASSWORD'));
// Create the client and session
$api = new BasicShopifyAPI($options);
$api->setSession(new Session($_SESSION['shop']));
// Now run your requests...
$result = $api->graph(...);
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
// ...
$options->setGuzzleOptions(['connect_timeout' => 3.0]);
// Create the client
$api = new BasicShopifyApi($options);
// Create options for the API
$options = new Options();
$options->setVersion('2020-01'); // YYYY-MM or "unstable" is accepted
// Create the client
$api = new BasicShopifyAPI($options);
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
// Create the client
$api = new BasicShopifyAPI($options);
$api->rest('GET', '/admin/api/unstable/shop.json'); // Will ignore "2020-01" version and use "unstable" for this request
$options = new Options();
// ...
$options->disableRateLimiting();
// ...
$api = new BasicShopifyAPI($options);
$api->addMiddleware(new CustomRateLimiter($api), 'rate:limiting');
$api->withSession(new Session('someshop.myshopify.com', 'some-token'), function (): void {
$request = $this->rest('GET', '/admin/shop.json');
echo $request['body']['shop']['name']; // Some Shop
});
// $api->rest/graph will not be affected by the above code, it will use previously defined session
// Create options for the API
$options = new Options();
$options->setVersion('2020-01');
// ...
$options->setGuzzleOptions(
'max_retry_attempts' => 3, // Was 2
'retry_on_status' => [429, 503, 400], // Was 439, 503, 500
);
// Create the client
$api = new BasicShopifyApi($options);
$call = $api->rest('GET', '/admin/non-existant-route-or-object.json');
if ($call['errors']) {
echo "Oops! {$call['status']} error";
var_dump($call['body']);
// Original exception can be accessed via `$call['exception']`
// Example, if response body was `{"error": "Not found"}`...
/// then: `$call['body']` would return "Not Found"
}
$api->addMiddleware([callable]);
$timeStore = new RedisStore();
$limitStore = new RedisStore();
$api = new BasicShopifyAPI($options, $timeStore, $limitStore);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.