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

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

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

vladimircatrici/shopify-api

Composer 安装命令:

composer require vladimircatrici/shopify-api

包简介

Library to operate with Shopify data via REST API

README 文档

README

This is a simple PHP library that provides a quick and easy way to work with Shopify REST API. It uses Guzzle as HTTP client.

Installation

composer require vladimircatrici/shopify

Usage

Initialization

require_once 'vendor/autoload.php';
use VladimirCatrici\Shopify;
Shopify\ClientManager::setConfig('default', [
    'domain' => 'your-shop-handle',
    'access_token' => 'your-access-token',
]);
$api = Shopify\ClientManager::get('default');

Configuration

There are a few additional options you can pass to the ClientManager.

  • api_version (default: the oldest stable supported version) The Shopify API version you want to use.
    Read more about API versioning at Shopify.

  • max_attempts_on_server_errors (default: 1)
    Number of attempts trying to execute the request. It's useful because sometimes Shopify may respond with 500 error. I would recommend set this to 2 or 3. The default value is 1 though.

  • max_attempts_on_rate_limit_errors (default: 1)
    Number of attempts trying to execute the request on getting 429 Too Many Connections error. This might be useful if the same API key is used by other apps which may lead to exceeding the rate limit. The recommended value would be somewhere under the 10.

  • max_limit_rate (default: 0.5)
    Number between 0 and 1 describing the maximum limit rate the client should reach before going sleep. See max_limit_rate_sleep_sec option.

  • max_limit_rate_sleep_sec (default: 1)
    Number of seconds to sleep when API reaches the maximum API limit rate specified in max_limit_rate option.

Basic usage

The client implements all 4 HTTP methods that Shopify REST API supports. Method names are:

  • get(string $endpoint, array $options)
  • post(string $endpoint, array $data)
  • put(string $endpoint, array $data)
  • delete(string $endpoint)

The $endpoint parameter must always be a string that represents a Shopify API endpoint. It should not contain the /admin/api/#{api_version}/ part in the beginning. The .json is not necessary in the end of the path as well. For example if Shopify documentations shows the endpoint path as GET /admin/api/#{api_version}/orders.json, you can just use:

$api->get('orders');

See more examples below:

Get items

$numProducts = $api->get('products/count'); // int
$products = $api->get('products'); // array
foreach ($products as $product) {
  echo sprintf('#%d. %s<br>', 
    $product['id'], $product['title']);
}

Get id and title fields of 250 items from the 2nd page

$products = $api->get('products', [
  'fields' => 'id,title',
  'limit' => 250,
  'page' => 2
]);

Get single item

$product = $api->get('products/123456789');
echo sprintf('#%d. %s', $product['id'], $product['title']);

Update item

$product = $api->put('products/123456789', [
    'title' => 'New title'
]);
echo sprintf('#%d. %s', $product['id'], $product['title']); // #1. New title

Create item

$product = $api->post('products', [
    'title' => 'New product' 
]);

Delete item

$api->delete('products/123456789');
if ($api->respCode == 200) {
    // Item has been successfully removed
}

Collection

You can use Collection object to get all the items from the specific endpoint. This library works fine with both page-based and cursor-based pagination and switches between them based on API version.

use VladimirCatrici\Shopify\ClientManager;
$api = ClientManager::get('default');
$products = new Collection($api, 'products');
foreach ($products as $product) {
    printf('#%d. %s [$%f], 
        $product['id'], $product['title'], $product['price']
    );
}

Webhooks

You can use this library to listen for shop's webhooks.

use VladimirCatrici\Shopify;
if (Shopify\Webhook::validate('your-webhook-token')) {
    printf('`%s` webhook triggered on your store (%s). Data received: %s', 
        Shopify\Webhook::getTopic(), 
        Shopify\Webhook::getShopDomain(),
        Shopify\Webhook::getData()
    );
} else {
    // Invalid request | Unauthorized webhook | Data corrupted
}

// You can also get webhook data as array right away
$data = Shopify\Webhook::getDataAsArray();
printf('Product ID#%d, product title: %s', 
    $data['id'], $data['title']
);

Troubleshooting

use VladimirCatrici\Shopify\Exception\RequestException;
try {
    $products = $api->get('products');
} catch (RequestException $e) {
    $request = $e->getRequest(); // PSR-7/Request object

    $response = $e->getResponse(); // PSR-7/Response object
    $code = $response->getStatusCode(); // int
    $headers = $response->getHeaders(); // array
    $bodyStream = $response->getBody(); // Stream (PSR-7/StreamInterface)
    $bodyContent = (string) $response->getBody(); // string (or $body->__toString())
    
    // Details of the errors including exception message, request and response details
    echo $e->getDetailsJson();
}

统计信息

  • 总下载量: 275
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2020-02-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固