rapidwebltd/etsy-php

基于Etsy Rest API描述输出,此封装提供了一个简单的客户端,包含Etsy API上所有可用的方法(归功于PHP的__call魔法方法!),并在每次请求时验证其参数。

0.9.2 2016-09-14 13:19 UTC

This package is auto-updated.

Last update: 2024-09-06 09:34:36 UTC


README

基于Etsy Rest API描述输出,此封装提供了一个简单的客户端,包含Etsy API上所有可用的方法(归功于PHP的__call魔法方法!),并在每次请求时验证其参数(查看https://github.com/rapidwebltd/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
    • 然后添加以下行到您的php.ini文件中:extension=oauth.so

安装

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

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

{
  "require": {
    "rapidwebltd/etsy-php": ">=0.9.0"
  }
}

用法

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

  • 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:一个包含方法所需post数据的数组

    示例: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 OS X上,它将自动打开您的默认浏览器)

生成的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

测试

$ vendor/bin/phpunit src/test/

感谢

这是Iñaki Abete良好工作的分支。