milindsingh / shopify-php-sdk
Shopify API 的 PHP SDK
v2.1.0
2017-12-12 03:37 UTC
Requires
- php: >=5.3.3
- ext-curl: *
- ext-json: *
- guzzlehttp/guzzle: ^6.2
- psr/log: ^1.0
Requires (Dev)
- overtrue/phplint: ^0.2.4
- phpunit/phpunit: ^6.1
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-09-12 03:52:33 UTC
README
此 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);
身份验证
Shopify API 的身份验证是通过 OAuth 获得的访问令牌完成的。为此,该客户端附带了一个帮助库
$client = new Shopify\Api($params); $helper = $client->getOAuthHelper(); $redirectUri = 'https://localhost/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; }