cedcommerce/etsy-php

简单的 Etsy API PHP 封装器

3.0.6 2024-02-15 06:25 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'
           )));
  # 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

贡献

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

请分支此项目并发送给我一个拉取请求!

许可

mobiledevice 在 MIT 许可下授权

www.opensource.org/licenses/MIT