zhangdaogui/etsy-laravel

Etsy PHP API for Laravel

dev-master 2019-10-29 07:55 UTC

This package is auto-updated.

Last update: 2024-09-29 05:35:39 UTC


README

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

使用了 etsy-php 的部分代码,作者是 Iñaki Abete

安装

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

composer require zhangdaogui/etsy-laravel

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

Zhangdaogui\Etsy\Providers\EtsyServiceProvider::class,

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

Zhangdaogui\Etsy\Facades\Etsy::class,

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

<?php

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

用法

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

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

    示例: 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 参考文档