charmquark / shopify-php
PHP中的简单Shopify API客户端
Requires
- php: >=5.3.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: 3.7.x
Suggests
- monolog/monolog: Add logging functionality
This package is not auto-updated.
Last update: 2024-09-28 17:49:11 UTC
README
PHP中的简单Shopify API客户端。
此开发流的规范仓库是
此API客户端仍处于预1.0状态,因此您可以期待
- 一些错误(请随时提交包含错误修复和测试覆盖的pull request)
- 在v0.9和v1.0之间可能会发生一些破坏性API更改
要求
- PHP 5.3(或更高版本)
- ext-curl, ext-json
开发要求
- phpunit/phpunit 3.7
入门
通过 Composer 安装 shopify-php
如果您的项目根目录中没有 composer.json
文件,请创建一个并添加 shopify-php 依赖项
{
"require": {
"offshoot/shopify-php": "0.9.x"
}
}
有关Composer的更多信息,包括完整的安装过程,请访问 https://getcomposer.org.cn/
使用cURL
如果您正在使用基于cURL的HttpClient,如 CurlHttpClient
,您可能希望包含可以从中找到 cacert.pem 文件的文件,该文件位于 http://curl.haxx.se/docs/caextract.html
您可以在composer文件中添加此依赖项。您的 composer.json
可能如下所示
{
"require": {
"offshoot/shopify-php": "0.9.x",
"haxx-se/curl": "1.0.0"
},
"repositories": [
{
"type": "package",
"package": {
"name": "haxx-se/curl",
"version": "1.0.0",
"dist": {
"url": "http://curl.haxx.se/ca/cacert.pem",
"type": "file"
}
}
}
]
}
您将在 vendor/haxx-se/curl/cacert.pem
中找到 cacert.pem 文件
用法
身份验证
如果您还没有Shopify API永久访问令牌,您将需要首先使用Shopify API进行身份验证
$pathToCertificateFile = "vendor/haxx-se/curl/cacert.pem";
$httpClient = new \Shopify\HttpClient\CurlHttpClient($pathToCertificateFile);
$redirector = new \Shopify\Redirector\HeaderRedirector();
$authenticate = new \Shopify\Api\AuthenticationGateway(
$httpClient, $redirector
);
$authenticate->forShopName('mycoolshop')
->usingClientId('XXX1234567890') // get this from your Shopify Account
->withScope(array('write_products', 'read_orders'))
->andReturningTo("http://wherever.you/like")
->initiateLogin();
这将使用户重定向到Shopify登录界面,他们需要使用Shopify凭证进行身份验证。完成此操作后,Shopify将对您的重定向URI执行GET请求,该URI将类似于
GET http://wherever.you/like?code=TEMP_TOKEN
您的应用程序需要从请求中捕获 code
查询参数,并使用该参数从Shopify获取永久访问令牌
$client = new Shopify\Api\Client($httpClient);
$client->setClientSecret('ABC123XYZ');
// validate the Shopify Request
if ($client->isValidRequest($_GET)) {
// exchange the token
$permanentAccessToken = $authenticate->forShopName('mycoolshop')
->usingClientId('XXX1234567890')
->usingClientSecret('ABC123XYZ')
->toExchange($_GET['code']);
}
TODO:将请求验证构建到交换过程中
TODO:让AuthenticationGateway扩展Api\Client
与Shopify API交互
一旦您有了有效的Shopify永久访问令牌,您就可以开始调用Shopify API了
首先设置Shopify API客户端的实例。
$client = new \Shopify\Api\Client($httpClient);
$client->setAccessToken($permanentAccessToken);
$client->setClientSecret('ABC123XYZ');
$client->setShopName('mycoolshop');
然后您可以开始与Shopify API交互。也许您想获取您商店中的所有产品
$products = $client->get('/admin/products.json', array(
'collection_id' => '987654321'
));
也许您想获取特定订单的详细信息
$order = $client->get('/admin/orders/123456789.json');
或者也许您想创建一个新的订单
$order = $client->post('/admin/orders.json', array(
'order' => array(
'line_items' => array(
0 => array(
'grams' => 1300,
'price' => 74.99,
'quantity' => 3,
'title' => "Big Brown Bear Boots",
),
),
'tax_lines' => array(
0 => array(
'price' => 29.25,
'rate' => 0.13,
'title' => "HST",
),
),
'transactions' => array(
0 => array(
'amount' => 254.22,
'kind' => "sale",
'status' => "success",
)
),
'total_tax' => 29.25,
'currency' => "CAD",
)
));
TODO:为PUT和DELETE方法编写测试
贡献
欢迎贡献。只需分支仓库并发送pull request。请确保在pull request中包含测试覆盖。您可以在此处了解更多关于Pull Requests的信息
为了运行测试套件,请确保已通过composer安装了开发依赖项。然后从您的命令行运行
vendor/bin/phpunit --bootstrap tests/bootstrap.php tests/
许可协议
此库是根据 MIT License 发布的
致谢
感谢 Sandeep Shetty 对初始代码库的开发。