Download the PHP package ohmybrew/basic-shopify-api without Composer
On this page you can find all versions of the php package ohmybrew/basic-shopify-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package basic-shopify-api
Basic Shopify API
A simple, tested, API wrapper for Shopify using Guzzle.
It supports both the sync/async REST and GraphQL API provided by Shopify, basic rate limiting, and request retries.
It contains helpful methods for generating a installation URL, an authorize URL (offline and per-user), HMAC signature validation, call limits, and API requests.
It works with both OAuth and private API apps.
Table of Contents
- Installation
- Usage
- Public API
- REST (sync)
- REST (async)
- GraphQL (sync)
- GraphQL (async)
- Getting access (offline)
- Getting access (per-user)
- Verifying HMAC signature
- Private API
- REST
- GraphQL
- Making requests
- REST
- If sync is true (regular rest call)
- If sync is false (restAsync call)
- Overriding request type
- Passing additional request options
- GraphQL
- REST
- API Versioning
- Rate Limiting
- page_info / pagination Support
- Isolated API calls
- Retries
- Errors
- Middleware
- Storage
- Public API
- Documentation
- LICENSE
Installation
The recommended way to install is through composer.
composer require gnikyt/basic-shopify-api
Usage
Public API
This assumes you properly have your app setup in the partner's dashboard with the correct keys and redirect URIs.
REST (sync)
For REST calls, the shop domain and access token are required.
REST (async)
For REST calls, the shop domain and access token are required.
GraphQL (sync)
For GraphQL calls, the shop domain and access token are required.
GraphQL (async)
For GraphQL calls, the shop domain and access token are required.
Getting access (offline)
This is the default mode which returns a permanent token.
After obtaining the user's shop domain, to then direct them to the auth screen use getAuthUrl
, as example (basic PHP):
Getting access (per-user)
You can also change the grant mode to be per-user
as outlined in Shopify documentation. This will receieve user info from the user of the app within the Shopify store. The token recieved will expire at a specific time.
Verifying HMAC signature
Simply pass in an array of GET params.
Private API
This assumes you properly have your app setup in the partner's dashboard with the correct keys and redirect URIs.
REST
For REST (sync) calls, shop domain, API key, and API password are request
GraphQL
For GraphQL calls, shop domain and API password are required.
Making requests
REST
Requests are made using Guzzle.
type
refers to GET, POST, PUT, DELETE, etcpath
refers to the API path, example:/admin/products/1920902.json
params
refers to an array of params you wish to pass to the path, examples:['handle' => 'cool-coat']
headers
refers to an array of custom headers you would like to optionally send with the request, example:['X-Shopify-Test' => '123']
sync
refers to if the request should be synchronous or asynchronous.
You can use the alias restAsync
to skip setting sync
to false
.
If sync is true (regular rest call):
The return value for the request will be an array containing:
response
the full Guzzle response objectbody
the JSON decoded response body (\Gnikyt\BasicShopifyAPI\ResponseAccess instance)errors
if any errors are detected, true/falseexception
if errors are true, exception object is availablestatus
the HTTP status codelink
an array of previous/next pagination values, if available
Note: request()
will alias to rest()
as well.
If sync is false (restAsync call):
The return value for the request will be a Guzzle promise which you can handle on your own.
The return value for the promise will be an object containing:
response
the full Guzzle response objectbody
the JSON decoded response body (\Gnikyt\BasicShopifyAPI\ResponseAccess instance)errors
if any errors are detected, true/falseexception
if errors are true, exception object is availablestatus
the HTTP status codelink
an array of previous/next pagination values, if available
Overriding request type
If you require the need to force a query string for example on a non-GET endpoint, you can specify the type as a key.
Valid keys are query
and json
.
Passing additional request options
If you'd like to pass additional request options to the Guzzle client created, pass them as the second argument of the constructor.
GraphQL
Requests are made using Guzzle.
query
refers to the full GraphQL queryvariables
refers to the variables used for the query (if any)
The return value for the request will be an object containing:
response
the full Guzzle response objectbody
the JSON decoded response body (\Gnikyt\BasicShopifyAPI\ResponseAccess instance)errors
if there was errors or not, will return the errors if any are foundstatus
the HTTP status code
Example query:
Example mutation:
API Versioning
This library supports versioning the requests, example:
You can override the versioning at anytime for specific API requests, example:
Rate limiting
This library comes with a built-in basic rate limiter which utilizes usleep
between applicable calls.
- For REST: it ensures you do not request more than the default of 2 calls per second.
- For GraphQL: it ensures you do not use more than the default of 50 points per second.
To adjust the default limits, use the option class' setRestLimit
and setGraphLimit
.
Custom rate limiting
You simply need to disable the built-in rate limiter and push in a custom Guzzle middleware. Example:
page_info / pagination support
2019-07 API version introduced a new Link
header which is used for pagination (explained here).
If an endpoint supports page_info, you can use $response->link
to grab the page_info value to pass in your next request.
Example:
Isolated API calls
You can initialize the API once and use it for multiple shops. Each instance will be contained to not pollute the others. This is useful for something like background job processing.
$this
will be binded to the closure. Example:
Retries
This library utilizes caseyamcl/guzzle_retry_middleware
middleware package.
By default, 429
, 500
and 503
errors will be retried twice.
For REST calls, it will utilize Shopify's X-Retry-After
header to wait x seconds before retrying the call.
When all retries are exhasted, the standard response from the library will return where you can handle the error.
To change the status codes watched or the maximum number of retries, use the option class' setGuzzleOptions
:
Errors
This library internally catches only 400-500 status range errors through Guzzle. You're able to check for an error of this type and get its response status code and body.
Middleware
This library takes advantage of using Guzzle middleware for request/response checks and modifications. You're also able to inject middleware.
See Guzzle's documentation on middleware. As well, you can browse this library's middleware for examples.
Storage
For storing the current request times, API limits, request costs, etc. A basic in-memory array store is used Gnikyt\BasicShopifyAPI\Store\Memory
.
If you would like to implement a more advananced store such as one with Redis, simply implement Gnikyt\BasicShopifyAPI\Contracts\StateStorage
and set the client to use it, example:
Documentation
Code documentation is available here from phpDocumentor via phpdoc -d src -t doc
.
LICENSE
This project is released under the MIT license.
Misc
Using Python? Check out basic_shopify_api.
All versions of basic-shopify-api with dependencies
ext-json Version *
caseyamcl/guzzle_retry_middleware Version ^2.3
guzzlehttp/guzzle Version ^6.5 || ^7.0