dan/shopify-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dan/shopify-api

Composer 安装命令:

composer create-project dan/shopify-api

包简介

Shopify API for PHP

README 文档

README

Please note: the new version using Guzzle 6.2 is actively maintained here

An object-oriented approach towards using the Shopify API.

Supported Objects / Endpoints:

Composer

$ composer require dan/shopify-api v0.9.9.*

Usage without Laravel

// Assumes setup of client with access token.
$mgr = ShopifyApi\Manager::init($shop, $token);
$mgr->getProduct($product_id = 123);              // returns ShopifyApi/Models/Product

// Alternatively, we may call methods on the API object.
$mgr->api('products')->show($product_id = 123);   // returns array

See Facade usages for other methods available.

Usage with Laravel

In your config/app.php

Add the following to your providers array:

ShopifyApi\Providers\ShopifyServiceProvider::class,

Add the following to your aliases array:

'Shopify' => ShopifyApi\Support\ShopifyFacade::class,

Replace following variables in your .env

SHOPIFY_DOMAIN=your-shop-name.myshopify.com
SHOPIFY_TOKEN=your-token-here

Using the Facade gives you Manager

Methods called on Manager will cascade down onto Client via the __call method.

If you're using Laravel, Models will return \Illuminate\Support\Collection instead of array.

Shopify::getProduct($product_id = 123);     // returns ShopifyApi/Models/Product
Shopify::getAllProducts();                  // returns Collection|array of ShopifyApi/Models/Product

Shopify::getVariant($variant_id = 456);     // returns ShopifyApi/Models/Variant
Shopify::getAllVariants($product_id = 123); // returns Collection|array of ShopifyApi/Models/Variant

Shopify::getOrder($order_id = 789);         // returns ShopifyApi/Models/Order
Shopify::getAllOrders();                    // returns a Collection|array of ShopifyApi/Models/Order

Shopify::getMetafield($metafield_id = 123); // returns ShopifyApi/Models/Metafield

Shopify::getDiscount($dicount_id = 123);    // returns ShopifyApi/Models/Discount
Shopify::getAllDiscounts();                 // returns Collection|array of ShopifyApi/Models/Discount

Shopify::getAllWebhooks();                  // returns Collection|array of ShopifyApi/Models/Webhook

// Alternatively, we may call methods on the API object.

Shopify::api('products')->show($product_id = 123);           // returns array
Shopify::api('products')->all();                             // returns array
Shopify::api('products')->count();                           // returns int

Shopify::api('products')->metafields($product_id = '1234')   // returns array

Shopify::api('variants')->show($variant_id = 456);           // returns array
Shopify::api('variants')->product($product_id = 123)->all(); // returns array

Shopify::api('orders')->show($order_id = 123);               // returns array
Shopify::api('orders')->all();                               // returns array
Shopify::api('orders')->count();                             // returns int

Shopify::api('custom_collections')->show($cc_id = 123);      // returns array
Shopify::api('custom_collections')->all();                   // returns array
Shopify::api('custom_collections')->count();                 // returns int

Shopify::api('discounts')->show($discount_id = 123);         // returns array
Shopify::api('discounts')->all();                            // returns array               
Shopify::api('discounts')->count();                          // returns int

Shopify::api('webhooks')->show($webhook_id = 123);           // returns array
Shopify::api('webhooks')->all();                             // returns array
Shopify::api('webhooks')->count();                           // returns int

Examples of saving data.

Creating a product using a model
$product = Shopify::getProduct();
$product->setTitle('Burton Custom Freestyle 151');
$product->setBodyHtml('<strong>Good snowboard!<\/strong>');
$product->setVendor('Burton');
$product->setProductType('Snowboard');
$product->setTags(['Barnes & Noble', 'John\'s Fav', '"Big Air"']);
$product->save();
Updating a product using a model
$product = Shopify::getProduct(123);
$product->setTitle('Burton Freestyle 152');
$product->save();
Add a product to a collection
$collection = Shopify::getCustomCollection(123);
$collection->add(456);

or

$collection = Shopify::getCustomCollection(123);
$collection->add([456,789]);
Creating and updating metafields for resources is more intuitive using key / namespace.
// The 'value_type' property will be determined automatically if omitted
$product->createMetafield('in_stock', 'inventory', ['value' => 123]); 

$product->updateMetafield('in_stock', 'inventory', ['value' => 122]);

$product->updateOrCreateMetafield('in_stock', 'inventory', ['value' => 200]);

// Support is included for arrays and objects (json encodable) and null
$product->createMetafield('board_specs', 'criteria', ['value' => new MyJsonSerializableObject()]);

Embedded Apps

Get a token for a redirect response.

Shopify::getAppInstallResponse(
    'your_app_client_id', 
    'your_app_client_secret',
    'shop_from_request',
    'code_from_request'
);

// returns (object) ['access_token' => '...', 'scopes' => '...']

Verify App Hmac (works for callback or redirect)

ShopifyApi\Util::validAppHmac(
    'hmac_from_request', 
    'your_app_client_secret', 
    ['shop' => '...', 'timestamp' => '...', ...]
);

Verify App Webhook Hmac

ShopifyApi\Util::validWebhookHmac(
    'hmac_from_request', 
    'your_app_client_secret', 
    file_get_contents('php://input')
);

Caching

If you want to sprinkle a little caching in, setup a service provider and extend the \ShopifyApi\Providers\ShopifyServiceProvider.

Here is an example Provider:

<?php

namespace App\Providers;

use App;
use ShopifyApi\Manager;
use ShopifyApi\Models\Product;
use ShopifyApi\Providers\ShopifyServiceProvider as BaseServiceProvider;

/**
 * Class ShopifyServiceProvider
 */
class ShopifyServiceProvider extends BaseServiceProvider
{
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        parent::register();

        /** @var Manager $shopify */
        $shopify = app('shopify');

        $shopify->setApiCache(Product::class, function($client, $params = null) {

            // No caching for collections.
            if (is_array($params)) {
                // Returning falsy will result in the default api behavior.
                return null;
            }
            
            $key = "shopify_product_".((string) $params);
            
            // Assuming you Cache::put($key, $product->getData()); elsewhere
            // If the cache is empty, the resource will be fetched from the api
            // as normal.
            $data = Cache::get($key);
            
            return $data ? new Product($client, $data) : null;
        });
    }
}

Contributors

Special Thanks

This repository's structure was modeled after the robust cdaguerre/php-trello-api.

Todo

  • Migrate from guzzle/guzzle to guzzlehttp/guzzle, bump version.
  • Publish files for Laravel setup
  • Artisan Command to create token

License

MIT.

dan/shopify-api 适用场景与选型建议

dan/shopify-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.32k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2016 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 21
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 21
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-16