slince / shopify-api-php
PHP Shopify API 客户端
3.1.0
2022-04-03 11:49 UTC
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-09-22 14:40:31 UTC
README
🚀 Shopify API 的 PHP SDK
安装
通过 Composer 安装
$ composer require slince/shopify-api-php
快速开始
初始化客户端
首先需要初始化客户端。为此,您需要您的商店名称和访问令牌
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 = function(\Psr\Http\Message\ServerRequestInterface $request, callable $next){ $response = $next($request); $this->logger->log($request, $response); return $response; }; $client->getMiddlewares()->push($middleware);
内置中间件
异常
客户端抛出以下类型的异常。
- BadRequestException 400
- UnauthorizedException 401
- PaymentRequiredException 402
- ForbiddenException 403
- NotFoundException 404
- NotAcceptableException 406
- UnprocessableEntityException 422
- TooManyRequestsException 429
- ClientException 其他
使用 Manager 来操作您的数据;
- 列出产品
$products = $client->getProductManager()->findAll([ // Filter your product 'collection_id' => 841564295 'page' => 2 // deprecated ]);
- 按分页列出产品
$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);
- 获取指定的产品
$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", ]);
- 创建一个新的产品
$product = $client->getProductManager()->create([ "title" => "Burton Custom Freestyle 151", "body_html" => "<strong>Good snowboard!<\/strong>", "vendor"=> "Burton", "product_type" => "Snowboard", ]);
- 通过其 ID 删除产品
$client->getProductManager()->remove(12800);
该产品是 Slince\Shopify\Manager\Product\Product
的实例;您可以像以下这样访问属性
echo $product->getTitle(); echo $product->getCreatedAt(); // DateTime Object //... print_r($product->getVariants()); print_r($product->getImages());
可用的 Manager
- 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
您可以像 $client->getProductManager()
、$client->getOrderManager()
这样访问 Manager。
基本的 CURD 操作
如果您不想使用 Manager,也可以像这样操作数据
返回值只是一个数组;
$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');
许可协议
MIT 许可协议。请参阅 MIT