hmalicha / etsy-php
简单的Etsy API PHP封装
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/php-code-coverage: 6.0.7
- phpunit/php-invoker: >=2.0.0
- phpunit/phpunit: 7.3.5
This package is auto-updated.
Last update: 2024-09-13 14:41:16 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
- Ubuntu:
- 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:一个包含方法所需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 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许可证