Download the PHP package codebyzach/cbz-shopify without Composer
On this page you can find all versions of the php package codebyzach/cbz-shopify. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codebyzach/cbz-shopify
More information about codebyzach/cbz-shopify
Files in codebyzach/cbz-shopify
Package cbz-shopify
Short Description PHP library for interacting with the Shopify REST & GraphQL API
License MIT
Homepage https://github.com/codebyzach/cbz-shopify
Informations about the package cbz-shopify
CbzShopify
CbzShopify is a modern PHP library based on Guzzle for the Shopify Admin API.
Dependencies
- PHP: ^7.3
- Guzzle Services: ^1.2
- Laminas Diactoros: ^2.6
Installation
Installation of CbzShopify is officially supported using Composer:
REST API
CbzShopify provides a one-to-one mapping with API methods defined in Shopify doc. Since version 4, it also supports a basic integration with the new GraphQL admin API.
Private app
In order to use CbzShopify as a private app, you must first instantiate the client:
Make sure to always include a version. More info about Shopify versioning
Public app
When using a public app, you instantiate the client a bit differently:
Make sure to always include a version. More info about Shopify versioning
Using a container
CbzShopify also provides built-in container-interop factories
that you can use. You must make sure that your container contains a service called "config" that is an array with a key
cbz_shopify
containing the required config:
If you're using Zend\ServiceManager 3, you can use Zend\ComponentInstaller to register our factories into Zend\ServiceManager automatically.
However if you're using other framework or other container, you can still manually register our factories, they are under src/Container folder.
Validating a request
CbzShopify client provides an easy way to validate an incoming request to make sure it comes from Shopify through the RequestValidator
object. It requires a PSR7 requests and a shared secret:
Validating a webhook
Similarily, you can use the WebhookValidator
to validate your webhook:
Validating an application request
Finally, you can also use the ApplicationProxyRequestValidator
to validate application proxy requests:
Create an authorization response
CbzShopify provides an easy way to create a PSR7 compliant ResponseInterface
to create an authorization response:
While the nonce
parameter is required, CbzShopify does not make any assumption about how to save the nonce and check it when
Shopify redirects to your server. You are responsible to safely saving the nonce.
Exchanging a code against an access token
You can use the TokenExchanger
class to exchange a code to a long-lived access token:
CbzShopify also provides a simple factory compliant with container-interop
that you can register to the container of your choice, with the CbzShopify\Container\TokenExchangerFactory
.
Exploiting responses
CbzShopify returns Shopify response directly. However, by default, Shopify wrap the responses by a top-key. For instance, if you want to retrieve shop information, Shopify will return this payload:
This is a bit inconvenient to use as we would need to do that:
Instead, CbzShopify automatically "unwraps" response, so you can use the more concise code:
When reading Shopify API doc, make sure you remove the top key when exploiting responses.
Count
Similarily, when you use one of the count
endpoint, CbzShopify will automatically extract the value from Shopify's response, so you do not need
to manually access the count property:
Using iterators
For most "list" endpoints (getProducts
, getCollections
...), Shopify allows you to get up to 250 resources at a time. When using the standard get**
method, you are responsible to handle the pagination yourself.
For convenience, CbzShopify allows you to easily iterate through all resources efficiently (internally, we are using generators). Here is how you can get all the products from a given store:
CbzShopify will take care of doing additional requests when it has reached the end of a given page.
Executing multiple requests concurrently
For optimization purposes, it may be desirable to execute multiple requests concurrently. To do that, CbzShopify client allow you to take advantage of the underlying Guzzle client and execute multiple requests at the same time.
To do that, you can manually create the Guzzle commands, and execute them all. CbzShopify will take care of authenticating all requests individually, and extracting the response payload. For instance, here is how you could get both shop info and products info:
If a request has failed, it will contain an instance of GuzzleHttp\Command\Exception\CommandException
. For instance, here is how you could iterate
through all the results:
GraphQL API
In 2018, Shopify launched a new API, called the GraphQL Admin API. This new API comes with a lot of advantages compared to the REST API:
- It allows to access more efficiently to the various Shopify resources (you can for instance get a collection, with all its products and variants, by using a single request).
- It offers access to some resources that are not exposed through the REST API.
The version 4 of CbzShopify now ships with a basic GraphQL client. It does not yet support the following features, though:
- Automatic pagination
- Automatic handling of Shopify rate limits
In order to use the client, you must instantiate it. Instead of the ShopifyClient, you must create a CbzShopify\ShopifyGraphQLClient
. If you are using
a private app:
Make sure to always include a version. More info about Shopify versioning
If you are using a public app:
Make sure to always include a version. More info about Shopify versioning
Queries
To perform query, simply enter your query as an heredoc. For instance, here is a GraphQL query that get the title and id of the first 5 collections, as well as the 5 first products within those collections (this used to require several queries in the REST API, while everything can be done very efficiently with GraphQL):
CbzShopify automatically unwrap the data
top key from Shopify response, so you can retrieves the data like this:
CbzShopify does not attempt to re-write the GraphQL response.
Variables
CbzShopify also fully supports GraphQL variable. For instance, here is how you can retrieve a given product by its ID by using GraphQL variables:
Mutations
Similarly, CbzShopify supports mutation. To do this, you simply need to use a mutation query. Here is an example that is creating a product:
This request will create a new product whose title is "My product", and will return the id of the product.
For better error handling, you should always include the userErrors object in your response.
Error handling
When using GraphQL requests, there are two kinds of errors that you can catch.
Request errors
Those errors are for malformed GraphQL requests. You can catch them using the \CbzShopify\Exception\GraphQLErrorException
exception:
User errors
Those errors are for requests that are missing data (like incorrect data, missing data...). You can catch them using the
\CbzShopify\Exception\GraphQLUserErrorException
exception:
Implemented endpoints
- Abandoned Checkouts
- AccessScope
- ApplicationCharge
- ApplicationCredit
- Article
- Asset
- AssignedFulfillmentOrder
- Balance
- Balance Transaction
- Blog
- CancellationRequest
- CarrierService
- Checkout
- Collect
- Collection
- CollectionListing
- Comment
- Country
- Currency
- CustomCollection
- Customer
- Customer Address
- CustomerSavedSearch
- Deprecated API Calls
- DiscountCode
- Dispute
- DraftOrder
- Event
- Fulfillment
- FulfillmentEvent
- FulfillmentOrder
- FulfillmentRequest
- FulfillmentService
- GiftCard
- InventoryItem
- InventoryLevel
- Location
- LocationsForMove
- MarketingEvent
- Metafield
- MobilePlatformApplication
- Order
- Order Risk
- Page
- Payment
- Payout
- Policy
- PriceRule
- Product
- Product Image
- Product ResourceFeedback
- Product Variant
- ProductListing
- Province
- RecurringApplicationCharge
- Redirect
- Refund
- Report
- ResourceFeedback
- ScriptTag
- ShippingZone
- Shop
- SmartCollection
- StorefrontAccessToken
- TenderTransaction
- Theme
- Transaction
- UsageCharge
- User
- Webhook
- Other Methods
- Iterator Methods
Abandoned Checkouts
AccessScope
ApplicationCharge
ApplicationCredit
Article
Asset
AssignedFulfillmentOrder
Balance
Balance Transaction
Blog
CancellationRequest
CarrierService
Checkout
Collect
Collection
CollectionListing
Comment
Country
Currency
CustomCollection
Customer
Customer Address
CustomerSavedSearch
Deprecated API Calls
DiscountCode
Dispute
DraftOrder
Event
Fulfillment
FulfillmentEvent
FulfillmentOrder
FulfillmentRequest
FulfillmentService
GiftCard
InventoryItem
InventoryLevel
Location
LocationsForMove
MarketingEvent
Metafield
MobilePlatformApplication
Order
Order Risk
Page
Payment
Payout
Policy
PriceRule
Product
Product Image
Product ResourceFeedback
Product Variant
ProductListing
Province
RecurringApplicationCharge
Redirect
Refund
Report
ResourceFeedback
ScriptTag
ShippingZone
Shop
SmartCollection
StorefrontAccessToken
TenderTransaction
Theme
Transaction
UsageCharge
User
Webhook
Other Methods
Iterator Methods
All versions of cbz-shopify with dependencies
guzzlehttp/guzzle-services Version ^1.2
psr/container Version ^2.0
psr/http-message Version ^1.0
laminas/laminas-diactoros Version ^2.6