gentor/etsy-php-laravel

Etsy的PHP包装器,用于Laravel

dev-master 2020-09-03 13:04 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:28 UTC


README

基于Etsy Rest API描述输出,此包装器提供了一个简单的客户端,包含Etsy API上的所有可用方法(归功于PHP的__call魔法方法),并在每个请求上验证其参数(查看methods.json以获取方法及其参数的完整列表)。

使用了一些来自etsy-php的代码,作者是Iñaki Abete

安装

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

composer require gentor/etsy-php-laravel

config/app.php中添加服务提供者

Gentor\Etsy\Providers\EtsyServiceProvider::class,

config/app.php中添加外观别名

Gentor\Etsy\Facades\Etsy::class,

复制配置文件,并在app/config/etsy.php中输入您的Etsy应用设置

<?php

return array(
    'consumer_key' => '',
    'consumer_secret' => '',
    'access_token' => '',
    'access_token_secret' => '',
    'scope' => ''
);

使用

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

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

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

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

    示例:createShippingTemplate:POST /shipping/templates

# it will request /shipping/templates sending the "data" array as the post data
Etsy::createShippingTemplate([
    'data' => [
        "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身份验证,因此我们需要令牌凭据(access_token和access_token_secret)。

// The $callbackUrl is the url of your app where Etsy sends the data needed for getting token credentials
$callbackUrl = 'http://your-app/etsy/approve';

// The $authorizationUrl is the Etsy url where the user approves your app
$authorizationUrl = Etsy::authorize($callbackUrl);

// On the callback endpoint run this code to get the token credentials and add them to your config
$tokenCredentials = Etsy::approve($request->get('oauth_token'), $request->get('oauth_verifier'));

return [
    'access_token' => $tokenCredentials->getIdentifier(),
    'access_token_secret' => $tokenCredentials->getSecret(),
];

示例

$shipping_template = [
    'data' => [
        "title" => "First API Template",
        "origin_country_id" => 209,
        "destination_country_id" => 209,
        "primary_cost" => 10.0,
        "secondary_cost" => 10.0
    ]
];
print_r(Etsy::createShippingTemplate($shipping_template));

# Upload image files:

$listing_image = [
    'params' => [
        'listing_id' => '152326352'
    ],
    'data' => [
        'image' => '/path/to/file.jpg'
    ]
];
print_r(Etsy::uploadListingImage($listing_image));

$listing_file = [
    'params' => [
        'listing_id' => '152326352'
    ],
    'data' => [
        'file' => '/path/to/file.jpg'
    ]
];
print_r(Etsy::uploadListingFile($listing_file));

关联

您将能够使用简单的接口检索资源的关联

$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 = Etsy::getListing($args);

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

JSON参数

有一些方法,Etsy要求将某些参数作为JSON字符串编码的参数(例如,“products”参数用于“updateInventory”)。在这些情况下,这些参数应定义如下

Etsy::updateInventory([
    'params' => [
        'listing_id' => '546557344'
    ],
    'data' => [
        'products' => [
            'json' => json_encode([
                [
                    'sku' => 'sku-1',
                    'property_values' => [
                        [
                            'property_id' => 200,
                            'values' => 'red'
                        ],
                        [
                            'property_id' => 52047899318,
                            'value' => '57 cm'
                        ]
                    ],
                    'offerings' => [
                        [
                            'price' => 10,
                            'quantity' => 3
                        ]
                    ]
                ],
                [
                    'sku' => 'sku-2',
                    'property_values' => [
                        [
                            'property_id' => 200,
                            'value' => 'red'
                        ],
                        [
                            'property_id' => 52047899318,
                            'value' => '68 cm'
                        ]
                    ],
                    'offerings' => [
                        [
                            'price' => 11,
                            'quantity' => 4
                        ]
                    ]
                ],
                [
                    'sku' => 'sku-3',
                    'property_values' => [
                        [
                            'property_id' => 200,
                            'value' => 'blue'
                        ],
                        [
                            'property_id' => 52047899318,
                            'value' => '57 cm'
                        ]
                    ],
                    'offerings' => [
                        [
                            'price' => 12,
                            'quantity' => 5
                        ]
                    ]
                ],
                [
                    'sku' => 'sku-4',
                    'property_values' => [
                        [
                            'property_id' => 200,
                            'value' => 'blue'
                        ],
                        [
                            'property_id' => 52047899318,
                            'value' => '68 cm'
                        ]
                    ],
                    'offerings' => [
                        [
                            'price' => 13,
                            'quantity' => 6
                        ]
                    ]
                ],
            ])
        ],
        'price_on_property' => [200, 52047899318],
        'quantity_on_property' => [200, 52047899318],
        'sku_on_property' => [200, 52047899318],
    ],
]);

文档

Etsy API参考