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部分中,我们将向商店添加新产品。
![title alt](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/01/14203035472000px-EBay_logo.svg-Custom.png)
依存关系 (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并没有真正融入MVC
的C
,因此我们还需要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-