Using ShopifyAPI
Getting an instance
use Esc\Shopify\API as ShopifyAPI;
// You can get an API instance via the IoC container
public function index(ShopifyAPI $api) {
}
public function index() {
// Or via the the app helper function
$api = app('ShopifyAPI');
}
Overriding the default config
You can override the default config using the setup function. If you're making a public app, you'll need to do this in order to specify which shop to access (by setting the domain and token):
$api = app('ShopifyAPI');
// Do authentication with $api
//...
$token = $result->access_token;
$domain = $result->shop_domain;
$api->setup([
'SHOP_DOMAIN' => $domain
'ACCESS_TOKEN' => $token
]);
// Do shop-specific things
// Or at creation time:
$api = app('ShopifyAPI', [
'SHOP_DOMAIN' => $myShop->shop_domain,
'ACCESS_TOKEN' => $myShop->access_token
]);
// Do shop-specific things
You can override any of the credentials keys.
You can also get an API object off of a Shop object if you're using the authentication helper framework:
$shop = \App\Shop::findByDomain('myshop.myshopify.com');
$api = $shop->getAPI();
$products = $api->call('get', '/admin/products.json')->products;
Making API calls
Format:
public function call($method = 'GET', $url = '/', $data = [], $options = [])
Options for $options
:
[
'charset' => 'UTF-8',
// Send additional headers with the request
'headers' => [],
// If true, throws a curl error. Otherwise, returns an exception.
'fail_on_error' => TRUE,
// If true, returns result as an array instead of an object.
'return_array' => FALSE,
// If true, returns _INFO and _ERROR properties on the result.
'all_data' => FALSE
'verify_data' => TRUE
]
Examples:
$res = $api->call('GET', '/admin/products.json');
foreach ($res->products as $product) {
echo $product->title;
}
/*
$res =>
{
"products": [
{
id: 49183274123,
...
}
...
]
}
*/
$res = $api->call('POST', '/admin/products.json', [
'product' => [
'title' => 'Example product',
'variants' => [
[
'price' => '15.00'
'sku' => 'U49213'
]
]
]
]);
echo $res->product->id;
/*
$res =>
{
"product": {
id: 49183274123,
...
}
}
*/