inakiabt/etsy-php

Etsy API 简单 PHP 封装

0.12.2 2020-04-17 18:46 UTC

README

基于 Etsy Rest API 描述 输出,此封装器提供了一个简单客户端,具有 Etsy API 上所有可用的方法(感谢 PHP 的 __call 魔术方法!),并在每个请求中验证其参数(有关方法及其参数的完整列表,请参阅 https://github.com/inakiabt/etsy-php/blob/master/src/Etsy/methods.json)。

需要帮助

最近,我无法投入我认为这个仓库应得的精力,因此我在寻找帮助!

要求

注意:我将致力于移除这些依赖项

  • cURL 开发
    • Ubuntu: sudo apt-get install libcurl4-dev
    • Fedora/CentOS: sudo yum install curl-devel
  • OAuth pecl 包
    • sudo pecl install oauth
    • 然后,将 extension=oauth.so 行添加到您的 php.ini

安装

以下推荐安装需要 composer。如果您不熟悉 composer,请参阅 composer 安装说明

将以下内容添加到您的 composer.json 文件中

{
  "require": {
    "inakiabt/etsy-php": ">=0.10"
  }
}

用法

所有方法只有一个参数,一个包含两个项的数组(两者都是可选的,取决于方法)

  • params:一个包含构建端点 URL 所需的所有必需参数的数组。

    示例:getSubSubCategory:GET /categories/:tag/:subtag/:subsubtag

  # it will request /categories/tag1/subtag1/subsubtag1
  $api->getSubSubCategory(array(
          'params' => array(
                         'tag' => 'tag1',
                         'subtag' => 'subtag1',
                         'subsubtag' => 'subsubtag1'
           )));
  • data:一个包含方法所需的数据的数组

    示例:createShippingTemplate:POST /shipping/templates

  # it will request /shipping/templates sending the "data" array as the post data
  $api->createShippingTemplate(array(
    						'data' => array(
   							    "title" => "First API Template",
   							    "origin_country_id" => 209,
   							    "destination_country_id" => 209,
   							    "primary_cost" => 10.0,
   							    "secondary_cost" => 10.0
           )));

OAuth 配置脚本

Etsy API 使用 OAuth 1.0 身份验证,因此让我们设置我们的凭据。

脚本 scripts/auth-setup.php 将生成 Etsy 客户端所需的 OAuth 配置文件,以执行签名请求。示例

export ETSY_CONSUMER_KEY=qwertyuiop123456dfghj
export ETSY_CONSUMER_SECRET=qwertyuiop12

php scripts/auth-setup.php /path/to/my-oauth-config-destination.php

它将显示一个您必须打开的 URL,在 Etsy 上登录并允许应用程序。然后,将验证码复制粘贴到终端。(在 Mac OSX 上,它将自动打开您的默认浏览器)

生成的 OAuth 配置文件

最终,它应该看起来像这样

<?php
 return array (
  'consumer_key' => 'df7df6s5fdsf9sdh8gf9jhg98',
  'consumer_secret' => 'sdgd6sd4d',
  'token_secret' => 'a1234567890qwertyu',
  'token' => '3j3j3h33h3g5',
  'access_token' => '8asd8as8gag5sdg4fhg4fjfgj',
  'access_token_secret' => 'f8dgdf6gd5f4s',
);

初始化

<?php
require('vendor/autoload.php');
$auth = require('/path/to/my-oauth-config-destination.php');

$client = new Etsy\EtsyClient($auth['consumer_key'], $auth['consumer_secret']);
$client->authorize($auth['access_token'], $auth['access_token_secret']);

$api = new Etsy\EtsyApi($client);

print_r($api->getUser(array('params' => array('user_id' => '__SELF__'))));

示例

print_r($api->createShippingTemplate(array(
 						'data' => array(
							    "title" => "First API Template",
							    "origin_country_id" => 209,
							    "destination_country_id" => 209,
							    "primary_cost" => 10.0,
							    "secondary_cost" => 10.0
							))));

# Upload local files: the item value must be an array with the first value as a string starting with "@":
$listing_image = array(
		'params' => array(
			'listing_id' => '152326352'
		),
		'data' => array(
			'image' => array('@/path/to/file.jpg;type=image/jpeg')
));
print_r($api->uploadListingImage($listing_image));

关联

您将能够使用简单界面获取给定资源的关联

    $args = array(
            'params' => array(
                'listing_id' => 654321
            ),
            // A list of associations
            'associations' => array(
                // Could be a simple association, sending something like: ?includes=Images
                'Images',
                // Or a composed one with (all are optional as Etsy API says) "scope", "limit", "offset", "select" and sub-associations ("associations")
                // ?includes=ShippingInfo(currency_code, primary_cost):active:1:0/DestinationCountry(name,slug)
                'ShippingInfo' => array(
                    'scope' => 'active',
                    'limit' => 1,
                    'offset' => 0,
                    'select' => array('currency_code', 'primary_cost'),
                    // The only issue here is that sub-associations couldn't be more than one, I guess.
                    'associations' => array(
                        'DestinationCountry' => array(
                            'select' => array('name', 'slug')
                        )
                    )
                )
            )
        );
   $result = $this->api->getListing($args);

有关关联的更多信息:https://www.etsy.com/developers/documentation/getting_started/resources#section_associations

JSON 参数

有些方法 Etsy 要求将参数编码为 JSON 字符串(例如,“variations”参数对于“createListingVariations”)。在这些情况下,这些参数应按如下方式定义

    $args = array(
        'params' => array(
            'listing_id' => 654321
        ),
        'data' => array(
          'variations' => array(
            'json' => json_encode(
                array(
                    array(
                        'property_id' => 200,
                        'value' => "Black"
                    ),
                    array(
                        'property_id' => 200,
                        'value' => "White"
                    )
                )
            )
        )
      )
    );

    $result = $this->api->createListingVariations($args);

测试

$ vendor/bin/phpunit

变更日志

  • 1.0
    • 初始化提交,工作模块。

作者

Iñaki Abete 网站: http://github.com/inakiabt 邮箱:inakiabt+github@gmail.com 推特:@inakiabt

贡献

发现了一个错误?想要贡献并添加新功能?

请将该项目分支出来,并发送给我一个 pull request!

许可证

mobiledevice 在 MIT 许可证下授权

www.opensource.org/licenses/MIT