ebay api_使用eBay交易API配置商店的设置

ebay apiIn part 1, we explained the different parts of eBay’s developer dashboard and configured our test application. We also created our database. Now we’re ready to create a project. In this part, ...
摘要由CSDN通过智能技术生成

ebay api

In part 1, we explained the different parts of eBay’s developer dashboard and configured our test application. We also created our database. Now we’re ready to create a project. In this part, we’ll focus on store settings. In part 3, we’ll add new products to our store.

在第1部分中,我们解释了eBay开发人员仪表板的不同部分,并配置了我们的测试应用程序。 我们还创建了数据库。 现在我们准备创建一个项目。 在这一部分中,我们将专注于商店设置。 在第3部分中,我们将向商店添加新产品。

alt

依存关系 (Dependencies)

Before we proceed, we first need to install the dependencies of the app. Create a composer.json file and add the following:

在继续之前,我们首先需要安装应用程序的依赖项。 创建一个composer.json文件并添加以下内容:

{
    "require" : {
        "slim/slim-skeleton": "dev-master",
        "slimcontroller/slimcontroller": "dev-master",
        "guzzlehttp/guzzle": "4.*",
        "vlucas/valitron": "~1.2",
        "codeguy/upload": "*"
    },
    "autoload": {
      "classmap": [
        "controllers"
      ]
    }
}

We will be using the Slim framework for this app. Since Slim doesn’t really have the C in MVC baked into it, we also need to require the Slim controller. Then we also require Guzzle so that we can easily make API calls to eBay. Next is valitron for form validation. Lastly, there’s the upload library for validating and processing uploads.

我们将为此应用程序使用Slim框架 。 由于Slim并没有真正融入MVCC ,因此我们还需要Slim控制器 。 然后,我们还需要Guzzle,以便我们可以轻松地对eBay进行API调用。 接下来是用于形式验证的valitron 。 最后,还有用于验证和处理上传的上传库。

Create an index.php file in the root of the project’s directory, then add the following code:

在项目目录的根目录中创建一个index.php文件,然后添加以下代码:

<?php
require 'vendor/autoload.php';

$app = New \SlimController\Slim(array(
    'templates.path' => 'templates'
));

$app->view(new \Slim\Views\Twig());
$app->view->parserOptions = array(
    'charset' => 'utf-8',
    'cache' => realpath('templates/cache'),
    'auto_reload' => true,
    'strict_variables' => false,
    'autoescape' => true
);

$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('baseUrl' => '/tester/ebay_trading_api'));
});

$app->run();

Breaking it down, first we require the autoload.php file so that we can use the libraries that we required in the composer.json file earlier.

分解它,首先我们需要autoload.php文件,以便我们可以使用在composer.json文件中需要的库。

require 'vendor/autoload.php';

Next, we initialize the Slim controller. This allows us to configure the options to be used by the controller such as the templates path which is the path to the templates directory.

接下来,我们初始化Slim控制器。 这使我们能够配置控制器要使用的选项,例如模板路径,即模板目录的路径。

$app = New \SlimController\Slim(array(
    'templates.path' => 'templates'
));

Next, we tell Slim to use Twig for handling our views:

接下来,我们告诉Slim使用Twig处理我们的视图:

$app->view(new \Slim\Views\Twig());
$app->view->parserOptions = array(
    'charset' => 'utf-8',
    'cache' => realpath('templates/cache'),
    'auto_reload' => true,
    'strict_variables' => true,
    'autoescape' => true
);

We also specified the following options:

我们还指定了以下选项:

  • charset – the character set to be used by Twig.

    字符集 – Twig使用的字符集。

  • cache – Twig compiles the templates that we’re using so it doesn’t have to do it on every page load. Those compiled template files are stored in the directory specified here.

    缓存 -Twig编译我们正在使用的模板,因此不必在每次页面加载时都这样做。 这些已编译的模板文件存储在此处指定的目录中。

  • auto_reload – this tells Twig to recompile the template every time the source code changes.

    auto_reload –告诉Twig每当源代码更改时重新编译模板。

  • strict_variables – this tells Twig to return errors when a variable used in the template has not been declared.

    strict_variables –告诉Twig当未声明模板中使用的变量时返回错误。

  • autoescape – this tells Twig to automatically escape html, CSS and JavaScript code if encountered in the template.

    autoescape –告诉Twig如果在模板中遇到,则自动转义html,CSS和JavaScript代码。

Next, we create a hook that would run before the Slim application is executed. What this hook does is attach the baseUrl to the view. This means that whatever value we assign to the baseUrl, it will always be accessible in every view. We use this value later on to link our front-end assets.

接下来,我们创建一个挂钩,该挂钩将在执行Slim应用程序之前运行。 该挂钩的作用是将baseUrl附加到视图。 这意味着无论我们分配给baseUrl值如何,在每个视图中始终可以访问它。 我们稍后将使用此值来链接我们的前端资产。

$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('baseUrl' => '/tester/ebay_trading_api'));
});

Finally, we run the Slim application:

最后,我们运行Slim应用程序:

$app->run();

易趣类 (Ebay Class)

We need to create a class that we can use to make calls to the API easier. Create a classes folder in the root of the project directory, then create a file and name it Ebay.php. Inside the file, add the following:

我们需要创建一个类,我们可以使用该类来简化对API的调用。 在项目目录的根目录中创建一个classes文件夹,然后创建一个文件并将其命名为Ebay.php 。 在文件内部,添加以下内容:

<?php
class Ebay {
}

Next declare the variables that we will use throughout the class:

接下来声明我们将在整个类中使用的变量:

public $compatability_level = 885;

public $sandbox_url = 'https://api.sandbox.ebay.com/ws/api.dll';
public $url = 'https://api.ebay.com/ws/api.dll';

public $run_name;
public $dev_id; 
public $app_id;
public $cert_id;
public $site_id;
public $user_token;

These are the same variables we had in the HTTP Headers in part 1. We need those variables on every request that we make to the API. The only thing that I haven’t introduced is the URL to which the requests are made. We declared two URLs in this class: one is for the live version of the API, and the other is for the sandbox version. You can just choose which one to use when making requests. But one thing you need to remember is that you can only use the sandbox URL for sandbox accounts, and the live URL for real eBay accounts. You cannot use these interchangeably.

这些是我们在第1部分HTTP标头中使用的变量。我们在对API的每个请求中都需要这些变量。 我唯一没有介绍的是向其发出请求的URL。 我们在此类中声明了两个URL:一个用于API的实时版本,另一个用于沙盒版本。 您可以仅在发出请求时选择使用哪一个。 但您需要记住的一件事是,只能将沙箱URL用于沙箱帐户,将实时URL用于真实的eBay帐户。 您不能将它们互换使用。

Next, create the constructor method. All it does is assign the global application settings so that we can use them throughout the class:

接下来,创建构造方法。 它所做的只是分配全局应用程序设置,以便我们可以在整个课程中使用它们:

public function __construct($settings){
  $this->run_name = $settings->run_name;
  $this->user_token = $settings->user_token;
  $this->dev_id = $settings->dev_id;
  $this->app_id = $settings->app_id;
  $this->cert_id = $settings->cert_id;
  $this->site_id = $settings->site_id;
}

Then, declare a request method. This would allow us to make the actual request to the API:

然后,声明一个request方法。 这将使我们能够向API发出实际请求:

public function request($method, $request_body){

  $client = new GuzzleHttp\Client();

  $response = $client->post($this->url, array(
      'verify' => false,
      'headers' => array(
          'X-EBAY-API-COMPATIBILITY-LEVEL' => $this->compatability_level,
          'X-EBAY-API-DEV-NAME' => $this->dev_id,
          'X-EBAY-API-APP-NAME' => $this->app_id,
          'X-EBAY-API-CERT-NAME' => $this->cert_id,        
          'X-EBAY-API-SITEID' => $this->site_id,
          'X-EBAY-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
支持自定数据库及多帐号 http://ebayapi.uhostnet.com 这里提供免费下载 ebay 的本地数据库自动同步软件 eBaySync, 就是用 eBay API GetSellerTransactions 做的,但特别的地方是它只提供本地数据库同步功能,而不提供后期的数据操作界面,可以让你把 ebay 上的交易 Transactions 完整的同步到本地任何的数据库内,它只做为一条数据同步的桥,让你不用认识和设计对 eBay API 的接口,而轻易灵活的可让eBay上的交易数据完整的同步到你们自己的 ERP SAP POS 等系统上又或只做数据统计,发货处理等工作上。。 免费使用步骤如下: 1。先在 api.ebaydev.com 免费注册一个用户资格 2。成功注册后,登录进去并加挂 eBay 帐号及授权令牌 3。下载 eBaySync.exe 并安装好 4。默认的数据会自动同步到安装目录下的 Database 内的 Transactions MS Access MDB 数据库上,但可以在软件上修改或改用其它数据库或服务器,包括支持任何的数据库系统,如 MSSQL MYSQL ACCESS 等,只要在 ODBC 上设定好连接的 DSN 并在软件上设定就可以了。数据表的模板可参考 Transactions.mdb 软件提供灵活的数据同步设定,可以设定多户口全自动同步,也可以设定要同步的数据字段,软件还是 Beta 版本,功能会一直加强及完善,欢迎提出建议。 软件下载位置:http://ebayapi.uhostnet.com/download/ebaydync-setup.exe 下一个版本将会提供 物流追踪码的上传同步 和在线产品下载同步的 功能上 欢迎任何查询连系QQ (1208455662)
以下是一个示例代码,可以批量修改 eBay Feed 中的库存信息: ```python import requests import xml.etree.ElementTree as ET # eBay API endpoint and credentials endpoint = 'https://api.ebay.com/ws/api.dll' dev_id = 'YOUR_DEV_ID' app_id = 'YOUR_APP_ID' cert_id = 'YOUR_CERT_ID' auth_token = 'YOUR_AUTH_TOKEN' site_id = '0' # US site ID # function to build eBay API request body def build_request_body(item_id, quantity): xml = '<?xml version="1.0" encoding="UTF-8"?>' xml += '<BulkDataExchangeRequests xmlns="urn:ebay:apis:eBLBaseComponents">' xml += '<Header><SiteID>{}</SiteID></Header>'.format(site_id) xml += '<Request><RequestID>1</RequestID>' xml += '<Item><ItemID>{}</ItemID>'.format(item_id) xml += '<Quantity>{}</Quantity></Item></Request>'.format(quantity) xml += '</BulkDataExchangeRequests>' return xml # function to make eBay API call def call_ebay_api(xml_request): headers = { 'X-EBAY-API-COMPATIBILITY-LEVEL': '967', 'X-EBAY-API-DEV-NAME': dev_id, 'X-EBAY-API-APP-NAME': app_id, 'X-EBAY-API-CERT-NAME': cert_id, 'X-EBAY-API-CALL-NAME': 'BulkDataExchange', 'X-EBAY-API-SITEID': site_id, 'X-EBAY-API-IAF-TOKEN': auth_token, 'Content-Type': 'text/xml' } response = requests.post(endpoint, headers=headers, data=xml_request) return response # example usage item_ids = ['ITEM_ID_1', 'ITEM_ID_2', 'ITEM_ID_3'] quantities = [10, 20, 30] for item_id, quantity in zip(item_ids, quantities): xml_request = build_request_body(item_id, quantity) response = call_ebay_api(xml_request) root = ET.fromstring(response.content) ack = root.find('.//{urn:ebay:apis:eBLBaseComponents}Ack').text if ack == 'Success': print('Successfully updated quantity for item {}'.format(item_id)) else: print('Failed to update quantity for item {}: {}'.format(item_id, response.content)) ``` 请注意,在使用此代码之前,您需要将代码中的 `YOUR_DEV_ID`、`YOUR_APP_ID`、`YOUR_CERT_ID` 和 `YOUR_AUTH_TOKEN` 替换为您自己的 eBay 开发者凭据。此外,您还需要将 `ITEM_ID_1`、`ITEM_ID_2` 和 `ITEM_ID_3` 替换为您要更新库存信息的 eBay 商品的实际 ID,以及 `10`、`20` 和 `30` 替换为相应的库存数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值