slince / shopify-api-php
Shopify API Client for PHP
Installs: 184 591
Dependents: 1
Suggesters: 0
Security: 0
Stars: 128
Watchers: 15
Forks: 48
Open Issues: 18
Requires
- php: ^7.2||^8.0
- doctrine/cache: ^1.0
- doctrine/inflector: ^1.2|^2.0
- guzzlehttp/guzzle: ^7.0
- jms/serializer: ^3.10
- slince/di: ^3.0
- symfony/yaml: ^4.2|^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^7.5|^8.0|^9.0
- symfony/filesystem: ^4.2|^5.0|^6.0
- symfony/property-access: ^4.2|^5.0|^6.0
This package is auto-updated.
Last update: 2024-10-22 15:02:17 UTC
README
🚀 PHP SDK for the Shopify API
Installation
Install via composer
$ composer require slince/shopify-api-php
Quick Start
Initialize the client
You first need to initialize the client. For that you need your Shop Name and AccessToken
require __DIR__ . '/vendor/autoload.php'; $credential = new Slince\Shopify\PublicAppCredential('Access Token'); // Or Private App $credential = new Slince\Shopify\PrivateAppCredential('API KEY', 'PASSWORD', 'SHARED SECRET'); $client = new Slince\Shopify\Client('your-store.myshopify.com', $credential, [ 'meta_cache_dir' => './tmp' // Metadata cache dir, required ]);
Middleware
Middleware augments the functionality of handlers by invoking them in the process of generating responses. Middleware is implemented as a higher order function that takes the following form.
$middleware = function(\Psr\Http\Message\ServerRequestInterface $request, callable $next){ $response = $next($request); $this->logger->log($request, $response); return $response; }; $client->getMiddlewares()->push($middleware);
Built-in middleware:
Exception
The Client throws the following types of exceptions.
- BadRequestException 400
- UnauthorizedException 401
- PaymentRequiredException 402
- ForbiddenException 403
- NotFoundException 404
- NotAcceptableException 406
- UnprocessableEntityException 422
- TooManyRequestsException 429
- ClientException other
Use Manager to manipulate your data;
- Lists products
$products = $client->getProductManager()->findAll([ // Filter your product 'collection_id' => 841564295 'page' => 2 // deprecated ]);
- Lists products by pagination
$pagination = $client->getProductManager()->paginate([ // filter your product 'limit' => 3, 'created_at_min' => '2015-04-25T16:15:47-04:00' ]); // $pagination is instance of `Slince\Shopify\Common\CursorBasedPagination` $currentProducts = $pagination->current(); //current page while ($pagination->hasNext()) { $nextProducts = $pagination->next(); } # to persist across requests you can use next_page_info and previous_page_info $nextPageInfo = $pagination->getNextPageInfo(); $prevPageInfo = $pagination->getPrevPageInfo(); $products = $pagination->current($nextPageInfo);
- Get the specified product
$product = $client->getProductManager()->find(12800); // Update the given product $product = $client->getProductManager()->update(12800, [ "title" => "Burton Custom Freestyle 151", "body_html" => "<strong>Good snowboard!<\/strong>", "vendor"=> "Burton", "product_type" => "Snowboard", ]);
- Creates a new product
$product = $client->getProductManager()->create([ "title" => "Burton Custom Freestyle 151", "body_html" => "<strong>Good snowboard!<\/strong>", "vendor"=> "Burton", "product_type" => "Snowboard", ]);
- Removes the product by its id
$client->getProductManager()->remove(12800);
The product is an instance of Slince\Shopify\Manager\Product\Product
; You can access properties like following:
echo $product->getTitle(); echo $product->getCreatedAt(); // DateTime Object //... print_r($product->getVariants()); print_r($product->getImages());
Available managers:
- Access\AccessScope
- Access\StorefrontAccessToken
- Analytics\Report
- Billing\ApplicationCharge
- Billing\ApplicationCredit
- Billing\RecurringApplicationCharge
- Billing\UsageCharge
- Customers\Address
- Customers\Customer
- Customers\CustomerSavedSearch
- Discounts\DiscountCode
- Discounts\PriceRule
- Events\Event
- Events\Webhook
- Inventory\InventoryItem
- Inventory\InventoryLevel
- Inventory\Location
- MarketingEvent\MarketingEvent
- OnlineStore\Article
- OnlineStore\Asset
- OnlineStore\Blog
- OnlineStore\Comment
- OnlineStore\Page
- OnlineStore\Redirect
- OnlineStore\ScriptTag
- OnlineStore\Theme
- Orders\DraftOrder
- Orders\Order
- Orders\Refund
- Orders\Risk
- Orders\Transaction
- Products\Collect
- Products\CustomCollection
- Products\Image
- Products\Product
- Products\SmartCollection
- Products\Variant
- Shipping\AssignedFulfillmentOrder
- Shipping\CarrierService
- Shipping\Fulfillment
- Shipping\FulfillmentOrder
- Shipping\FulfillmentService
- Store\Country
- Store\Currency
- Store\Policy
- Store\Province
- Store\ShippingZone
- Store\Shop
You can access the manager like $client->getProductManager()
, $client->getOrderManager()
.
Basic CURD
If you don't like to use managers, you can also manipulate data like this:
The returned value is just an array;
$products = $client->get('products', [ // Filter your products ]); $product = $client->get('products/12800'); $product = $client->post('products', [ "product" => [ "title" => "Burton Custom Freestyle 151", "body_html" => "<strong>Good snowboard!<\/strong>", "vendor"=> "Burton", "product_type" => "Snowboard", "images" => [ [ "attachment" => "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAw==\n" ] ] ] ]); $product = $client->put('products/12800', [ "product" => [ "title" => "Burton Custom Freestyle 151", "body_html" => "<strong>Good snowboard!<\/strong>", "vendor"=> "Burton", "product_type" => "Snowboard", "images" => [ [ "attachment" => "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAw==\n" ] ] ] ]); $client->delete('products/12800');
LICENSE
The MIT license. See MIT