robwittman / shopify-php-sdk
Shopify API 的 PHP SDK
v2.4.2
2021-03-07 00:45 UTC
Requires
- php: >=7.2
- ext-curl: *
- ext-json: *
- guzzlehttp/guzzle: ^6.2 | ^7.2
- psr/log: ^1.0
Requires (Dev)
- overtrue/phplint: ^0.2.4
- phpunit/phpunit: ^5.1
- squizlabs/php_codesniffer: ^3.0
- dev-master
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-fix-composer-json
- dev-add-status-field-product
- dev-fix-webhooks-endpoints
- dev-move-create-service
- dev-fix-graphql-service
- dev-add-service-helpers
- dev-add-attachment-to-product-image
- dev-fix-shop-url
- dev-fix-api-version-url
- dev-fix-api-version
- dev-add-price-rule-service
This package is auto-updated.
Last update: 2024-09-07 09:04:14 UTC
README
Shopify PHP SDK
此 SDK 的创建是为了能够快速高效地使用 Shopify 的 API 进行开发。
安装
使用 composer 轻松安装此包
composer require robwittman/shopify-php-sdk
在您开始使用此 SDK 之前,您必须创建一个 Shopify 应用。您现在可以使用 API 密钥和密钥来生成访问令牌,然后可以使用这些令牌访问商店的数据
初始化
初始化 Api 客户端
$client = new Shopify\Api(array( 'api_key' => '<api_key>', 'api_secret' => '<api_secret>', 'myshopify_domain' => 'store.myshopify.com', 'access_token' => '<store_access_token>' ));
如果您正在使用用于个人商店的私有应用
$client = new Shopify\PrivateApi(array( 'api_key' => '<api-key>', 'password' => '<password>', 'shared_secret' => '<shared-secret>', 'myshopify_domain' => '<store>.myshopify.com' ));
客户端初始化后,您就可以创建一个服务,并使用它来与 API 通信
读取
$service = new Shopify\Service\ProductService($client); $service->all(); #Fetch all products, with optional params $service->get($productId); # Get a single product $service->count(); # Count the resources
创建
$service = new Shopify\Service\ProductService($client); $product = new Shopify\Object\Product(); # Set some product fields $product->title = 'Test Product'; $product->vendor = 'Printer'; $service->create($product);
更新
$service = new Shopify\Service\ProductService($client); $product = $service->get($productId); # Set some product fields $product->title = 'Test Product'; $product->vendor = 'Printer'; $service->update($product);
删除
$service = new Shopify\Service\ProductService($client); $service->delete($productId);
GraphQL
查询
$service = new Shopify\Service\GraphQLService($client); $service->graph( '{ products(query: "created_at:<2019", first: 5) { edges { node { title description } } } }' );
变更
$service = new Shopify\Service\GraphQLService($client); $service->graph( 'mutation productCreate($input: ProductInput!){ productCreate(input: $input) { product { id } } }', ['input' => ['title' => 'Sweet new product','productType' => 'Snowboard','vendor' => 'JadedPixel']] );
身份验证
通过 OAuth 获取的访问令牌进行 Shopify API 的身份验证。此客户端包含一个帮助库,用于获取令牌
$client = new Shopify\Api($params); $helper = $client->getOAuthHelper(); $redirectUri = 'https:///install.php'; $scopes = 'write_products,read_orders,...'; $authorizationUrl = $helper->getAuthorizationUrl($redirectUri, $scopes); header("Location: {$authorizationUrl}");
在您的 redirect_uri
上重新实例化帮助程序以获取访问令牌
$client = new Shopify\Api($params); $helper = $client->getOAuthHelper(); $token = $helper->getAccessToken($code); echo $token->access_token; echo $token->scopes;
默认情况下,这使用简单的会话存储。您可以实现一个实现 PersistentStorageInterface
的自定义类,将其传递给 new Shopify\Api()
,然后 OAuthHelper
将使用该类。如果授权请求和重定向可能会被重定向到不同的服务器,则需要这样做。
使用对象
可以使用 object->property
访问对象属性。嵌套对象是实例化的类。所有时间戳字段都是 \DateTime
的实例。
use Shopify\Enum\Fields\ProductFields; use Shopify\Enum\Fields\ProductVariantFields; $product = $service->get($productId); echo $product->created_at->format('Y-m-d H:i:s'); echo $product->title; foreach ($product->variants as $variant) { echo $variant->option1; echo $variant->option2; }