定制 offshoot/shopify-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

offshoot/shopify-php

最新稳定版本:0.10.0

Composer 安装命令:

composer require offshoot/shopify-php

包简介

Simple Shopify API client in PHP

关键字:

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

A simple Shopify API client in PHP.

The canoncial repository for this stream of development is https://github.com/TeamOffshoot/shopify-php

This API Client is still in a pre-1.0 state, so you can expect:

  • some bugs (feel free to submit a pull request with bug fixes and test coverage)
  • possibly some breaking API changes between v0.10 and v1.0

Requirements

  • PHP 5.3 (or higher)
  • ext-curl, ext-json

Development Requirements

  • phpunit/phpunit 3.7

Getting Started

Install shopify-php via Composer

Create a composer.json file if you don't already have one in your projects root directory and require shopify-php:

{
  "require": {
    "offshoot/shopify-php": "0.10.x"
  }
}

To learn more about Composer, including the complete installation process, visit http://getcomposer.org/

Using cURL

If you're using a cURL based HttpClient like the CurlHttpClient, you will want to include the cacert.pem file that can be found at http://curl.haxx.se/docs/caextract.html

You can add this as a dependency in your composer file. Your composer.json might look something like this:

{
  "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"
        }
      }
    }
  ]
}

You will be able to find the cacert.pem file in vendor/haxx-se/curl/cacert.pem

Usage

Authentication

If you do not already have a Shopify API Permanent Access Token, you will need you authenticate with the Shopify API first

$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();

This will redirect your user to a Shopify login screen where they will need to authenticate with their Shopify credentials. After doing that, Shopify will perform a GET request to your redirect URI, that will look like:

GET http://wherever.you/like?code=TEMP_TOKEN

Your application will need to capture the code query param from the request and use that to get the permanent access token from 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: build request validation into exchange process

TODO: have AuthenticationGateway extend Api\Client

Interacting with the Shopify API

Once you have a valid Shopify Permanent Access Token, you can start making calls to the Shopify API

First setup an instance of the Shopify API client.

$client = new \Shopify\Api\Client($httpClient);
$client->setAccessToken($permanentAccessToken);
$client->setClientSecret('ABC123XYZ');
$client->setShopName('mycoolshop');

Then you're ready to start interacting with the Shopify API. Maybe you want to get all of the products from your store

$products = $client->get('/admin/products.json', array(
    'collection_id' => '987654321'
));

Maybe you want to get the details of a specific order

$order = $client->get('/admin/orders/123456789.json');

Or maybe you want to create a new Order

$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: Implement PUT and DELETE functionality

Contributing

Contributions are welcome. Just fork the repository and send a pull request. Please be sure to include test coverage with your pull request. You can learn more about Pull Requests here

In order to run the test suite, ensure that the development dependencies have been installed via composer. Then from your command line, simple run:

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

License

This library is released under the MIT License

Acknowledgements

Thanks to Sandeep Shetty for his development of the initial code base.

offshoot/shopify-php 适用场景与选型建议

offshoot/shopify-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.7k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2014 年 01 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「shopify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 offshoot/shopify-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 offshoot/shopify-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 offshoot/shopify-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 6
  • Forks: 88
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-01-27