1. Go to this page and download the library: Download seka19/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/ */
seka19 / basic-shopify-api example snippets
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('your shop here');
$api->setAccessToken('your token here');
// Now run your requests...
$resul = $api->rest(...);
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('your shop here');
$api->setAccessToken('your token here');
// Now run your requests...
$promise = $api->restAsync(...);
$promise->then(function ($result) {
// ...
});
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('your shop here');
$api->setAccessToken('your token here');
// Now run your requests...
$api->graph(...);
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop($_SESSION['shop']);
$api->setApiKey(env('SHOPIFY_API_KEY'));
$api->setApiSecret(env('SHOPIFY_API_SECRET'));
$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);
// Above is equiv. to:
//
// $access = $api->requestAccess($code);
// $api->setAccessToken($access->access_token);
//
// You can use: $api->getAccessToken() and set it into the database or a cookie, etc
// You can now make API callsn`
$request = $api->rest('GET', '/admin/shop.json'); // or GraphQL
}
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop($_SESSION['shop']);
$api->setApiKey(env('SHOPIFY_API_KEY'));
$api->setApiSecret(env('SHOPIFY_API_SECRET'));
$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);
// Above is equiv. to:
//
// $access = $api->requestAccess($code);
// $api->setAccessToken($access->access_token);
// $api->setUser($access->associated_user)
//
// You can use: $api->getAccessToken() and set it into a cookie, etc
// You can also get user details with: $api->getUser(), example: $api->getUser()->email
// 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);
$api = new BasicShopifyAPI(true); // true sets it to private
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('example.myshopify.com');
$api->setApiKey('your key here');
$api->setApiPassword('your password here');
// Now run your requests...
$result = $api->rest(...);
$api = new BasicShopifyAPI(true); // true sets it to private
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('example.myshopify.com');
$api->setApiPassword('your password here');
// Now run your requests...
$api->graph(...);
$api = new BasicShopifyAPI(true);
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
// ... your code
$api = new BasicShopifyAPI(true);
$api->setVersion('2019-04');
$api->rest('GET', '/admin/api/unstable/shop.json'); // Will ignore "2019-04" version and use "unstable" for this request
// ... your code
// Returns an array of left, made, and limit.
// Example: ['left' => 79, 'made' => 1, 'limit' => 80]
$limits = $api->getApiCalls('rest'); // or 'graph'
// As example, this will return 79
// You may pass 'left', 'made', or 'limit'
$left = $api->getApiCalls('graph', 'left'); // returns 79
// or
$left = $api->getApiCalls('graph')['left']; // returns 79
$api = new BasicShopifyAPI(true);
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setApiKey('your key here');
$api->setApiPassword('your password here');
$api->withSession('some-shop.myshopify.com', 'token from database?', function() {
$request = $this->rest('GET', '/admin/shop.json');
echo $request->body->shop->name; // Some Shop
});
$api->withSession('some-shop-two.myshopify.com', 'token from database?', function() {
$request = $this->rest('GET', '/admin/shop.json');
echo $request->body->shop->name; // Some Shop Two
});
$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->setLogger(... your logger instance ...);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.