shoppingfeed/php-sdk

Shopping Feed SDK 用于简化我们API的集成

0.11.0 2024-09-19 12:00 UTC

README

安装

  1. 在你的项目根仓库运行
    composer require shoppingfeed/php-sdk
  2. 安装你选择的http客户端,我们推荐使用GuzzleHttp 6,因为SDK集成了对该客户端的适配器。
    composer require guzzlehttp/guzzle ^6.3
    如果你已经在项目中有一个http客户端库,你也可以开发自己的适配器(更多信息请参阅http适配器文档)。

这将把SDK库加载到vendor仓库中。
根据PSR-4规范,你应该能够在\ShoppingFeed\Sdk命名空间下访问SDK。

基本使用

使用SDK的基本有三个步骤:

  1. 身份验证以启动新会话
  2. 从会话中检索你想要管理的店铺
  3. 管理资源

API身份验证

Shopping Feed API要求你必须进行身份验证才能执行任何调用。

为了执行认证调用,你应该像这样构建客户端

<?php
namespace ShoppingFeed\Sdk;

// Setup credentials to connect to the API, and create session
$credential = new Credential\Token('api-token');
/** @var \ShoppingFeed\Sdk\Api\Session\SessionResource $session */
$session = Client\Client::createSession($credential);

访问你的店铺

/** @var \ShoppingFeed\Sdk\Api\Session\SessionResource $session */
$store = $session->selectStore(1276);
$store->getName(); // test-store
$store->getId(); // 1276
// ... and so on

如果你管理多个店铺,你可以使用店铺集合对象

/** @var \ShoppingFeed\Sdk\Api\Session\SessionResource $session */
// Get store collection
$stores = $session->getStores();
// Count the number of stores [int]
$stores->count();
// Get a particular store
$store = $stores->select('id-or-store-name');
// Loop over available stores
foreach ($stores as $store) {
    $store->getName(); 
}

SDK指南

SDK资源文档

生成符合XML规范的导入用feed

SDK能够通过提供必要的工具来简化XML feed的创建。

请查看https://github.com/shoppingflux/php-feed-generator上的文档,了解如何创建符合规范的feed。