cdyweb/shopify-php

PHP中的简单Shopify API客户端

0.10.0 2015-06-01 06:51 UTC

This package is not auto-updated.

Last update: 2024-10-02 08:27:07 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

PHP中的简单Shopify API客户端。

此开发流线的官方仓库为https://github.com/TeamOffshoot/shopify-php

此API客户端仍处于预1.0状态,因此您可以期待

  • 一些错误(请随时提交带有错误修复和测试覆盖的pull request)
  • 在v0.10和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.10.x"
  }
}

要了解更多关于Composer的信息,包括完整的安装过程,请访问https://composer.php.ac.cn/

使用cURL

如果您使用的是基于cURL的HttpClient,如CurlHttpClient,则希望包含可从http://curl.haxx.se/docs/caextract.html找到的cacert.pem文件

您可以在composer文件中添加此依赖项。您的composer.json可能看起来像这样

{
  "require": {
    "offshoot/shopify-php": "0.10.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的更多信息here

为了运行测试套件,请确保已通过composer安装了开发依赖项。然后从您的命令行,简单地运行

vendor/bin/phpunit --bootstrap tests/bootstrap.php tests/

许可协议

此库根据MIT License发布

鸣谢

感谢Sandeep Shetty为其初始代码库的开发。