ebay api

tech2022-09-16  98

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部分中,我们将向商店添加新产品。

依存关系 (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-API-CALL-NAME' => $method ), 'body' => $request_body )); $body = $response->xml(); return $body; }

This method accepts two arguments: the $method and the $request_body. The $method is the name of the API method that you want to call. Here you can find a list of API methods that you can use in the eBay Trading API. The other argument is the $request_body, which is an XML string that contains the data required by the API method. You can also find detailed information on the data that you need to pass for each API method in the link that I’ve provided so be sure to check that out.

此方法接受两个参数: $method和$request_body 。 $method是您要调用的API方法的名称。 在这里,您可以找到可以在eBay Trading API中使用的API方法的列表 。 另一个参数是$request_body ,它是一个XML字符串,其中包含API方法所需的数据。 您还可以在我提供的链接中找到有关每种API方法需要传递的数据的详细信息,因此请务必检查一下。

Inside the method, we declare a new instance of the GuzzleHTTP client.

在该方法内部,我们声明GuzzleHTTP客户端的新实例。

$client = new GuzzleHttp\Client();

Next, we make a POST request to the sandbox URL. We pass in an array which contains the request headers and body:

接下来,我们向沙盒URL发出POST请求。 我们传入一个包含请求标头和正文的数组:

$response = $client->post($this->sandbox_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-API-CALL-NAME' => $method ), 'body' => $request_body ));

We have set verify to false. This sets the SSL certificate verification to false. Note that this is insecure, so it is preferred to set this to true. But since we’re just in the testing phase, we can set it to false to avoid the SSL certificate check.

我们将verify设置为false 。 这会将SSL证书验证设置为false 。 请注意,这是不安全的,因此最好将其设置为true 。 但是由于我们正处于测试阶段,因此可以将其设置为false以避免SSL证书检查。

Next is headers – these are basically the same request headers that we saw earlier.

接下来是headers -这些基本上是我们之前看到的相同的请求标头。

Lastly, there’s the body which contains the body of the request. We will see later the kind of data we need to pass to it.

最后,还有的body含有请求的主体。 稍后我们将看到需要传递给它的数据类型。

Once the request returns a response it will be in XML format, so we tell Guzzle to parse it for us by calling the xml() method. This is pretty much like calling simplexml_load_string to convert XML into an object. Finally, we call json_encode and json_decode to convert the object into an array.

请求返回响应后,它将采用XML格式,因此我们告诉Guzzle通过调用xml()方法为我们解析它。 这非常类似于调用simplexml_load_string将XML转换为对象。 最后,我们调用json_encode和json_decode将对象转换为数组。

Next, create the method which will retrieve the session ID from the API. For this, we need to use the GetSessionID method from the API. This requires the run name of the app as its parameter:

接下来,创建将从API检索会话ID的方法。 为此,我们需要使用API​​中的GetSessionID方法。 这需要应用程序的运行名称作为其参数:

public function getSessionID(){ $request_body = '<?xml version="1.0" encoding="utf-8"?> <GetSessionIDRequest xmlns="urn:ebay:apis:eBLBaseComponents"> <RuName>' . $this->run_name . '</RuName> </GetSessionIDRequest>'; $response = $this->request('GetSessionID', $request_body); return $response->SessionID; }

Next is the getUserToken method which uses the FetchToken API method. This requires the session ID which we get from calling getSessionID. What this method does is return the user token which we can use to make requests to the API on behalf of the user.

接下来是使用FetchToken API方法的getUserToken方法。 这需要我们通过调用getSessionID获得的会话ID。 此方法的作用是返回用户令牌,我们可以使用该令牌代表用户向API发出请求。

public function getUserToken($session_id){ $request_body = '<?xml version="1.0" encoding="utf-8"?> <FetchTokenRequest xmlns="urn:ebay:apis:eBLBaseComponents"> <SessionID>' . $session_id . '</SessionID> </FetchTokenRequest>'; $response = $this->request('FetchToken', $request_body); return $response->eBayAuthToken; }

The flow would be something like this:

流将是这样的:

We call getSessionID which returns the session ID.

我们调用getSessionID ,它返回会话ID。

We set the app to redirect to eBay passing the session ID into the redirect URL.

我们将应用程序设置为重定向到eBay,并将会话ID传递到重定向URL。 User logs in and gives permission to the app.

用户登录并授予该应用程序的权限。 User is redirected to the app.

用户被重定向到应用程序。

We call the getUserToken to get the user token, passing in the session ID we got earlier.

我们调用getUserToken来获取用户令牌,并传递我们之前获得的会话ID。

API returns the user token and we save it to the database for later use.

API返回用户令牌,我们将其保存到数据库中供以后使用。

Later on, we’re going to implement it in our code.

稍后,我们将在代码中实现它。

That’s pretty much all we need for now from this class. Now we need to tell Composer to autoload the class for us. Open up composer.json and add classes in the classmap property of autoload:

这几乎是我们目前在该课程中需要的全部。 现在我们需要告诉Composer为我们自动加载该类。 打开composer.json并在autoload的classmap属性中添加classes :

"autoload": { "classmap": [ "controllers", "classes" ] }

Next open up the index.php file in the root directory of the project and add the following code right before $app->run(). This would allow us to call methods from the Ebay class from anywhere using $this->app->ebay:

接下来,在项目的根目录中打开index.php文件,并在$app->run()之前添加以下代码。 这将使我们可以使用$this->app->ebay从任何地方调用Ebay类中的方法:

$app->container->singleton('ebay', function () use($app) { $id = 1; $settings_result = $app->db->query("SELECT user_token, run_name, dev_id, app_id, cert_id, site_id FROM settings WHERE id = $id"); $settings = $settings_result->fetch_object(); return new Ebay($settings); });

While we’re here, let’s also assign a mysqli instance to the app container and name it db.

在这里时,我们还要为应用容器分配一个mysqli实例并将其命名为db 。

$app->container->singleton('db', function (){ $server = 'localhost'; $user = 'user'; $pass = ''; $database = 'ebaytrading'; return new mysqli($server, $user, $pass, $database); });

主页 (Home Page)

Now we’re ready to start working on the front-end side of things. First, create a base.twig file under the templates directory, then add the following code:

现在,我们准备开始在前端方面工作。 首先,在templates目录下创建一个base.twig文件,然后添加以下代码:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{ title }}</title> <link rel="stylesheet" href="{{ baseUrl }}/assets/lib/bootstrap/bootstrap.css"> {% if is_productpage %} <link rel="stylesheet" href="{{ baseUrl }}/assets/lib/dropzone/dropzone.css"> {% endif %} </head> <body> <div class="navbar navbar-default navbar-static-top" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">{{ title }}</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="{{ baseUrl }}">Home</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Products <span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ baseUrl }}/products/new">New</a></li> <li><a href="{{ baseUrl }}/products">List</a></li> </ul> </li> </ul> <ul class="nav navbar-nav"> <li class="dropdown"><a href="{{ baseUrl }}/token">Get Token</a></li> </ul> <ul class="nav navbar-nav"> <li><a href="{{ baseUrl }}/settings">Settings</a></li> </ul> </div> </div> </div> <div class="container"> {% block content %}{% endblock %} </div> <script src="{{ baseUrl }}/assets/js/jquery.js"></script> <script src="{{ baseUrl }}/assets/lib/bootstrap/bootstrap.js"></script> {% if is_productpage %} <script src="{{ baseUrl }}/assets/js/handlebars.js"></script> <script src="{{ baseUrl }}/assets/lib/dropzone/dropzone.js"></script> <script src="{{ baseUrl }}/assets/js/dropzone-options.js"></script> <script src="{{ baseUrl }}/assets/js/new-product.js"></script> {% endif %} </body> </html>

If this template looks familiar, that is because were using Twitter Bootstrap for this project.

如果这个模板看起来很熟悉,那是因为该项目正在使用Twitter Bootstrap。

Going back to the template, we’re using three variables: baseUrl, content, and title. Earlier, we have already declared the baseUrl from the index.php file. Later on, we’re going to pass in the content and the title from the controller. For now, let’s focus on blocks in Twig. We use them in the template by calling block and then the name of the variable. In this case we’ve called the block content:

回到模板,我们使用三个变量: baseUrl , content和title 。 之前,我们已经从index.php文件中声明了baseUrl 。 稍后,我们将传递控制器的content和title 。 现在,让我们关注Twig中的blocks 。 我们通过调用block然后是变量的名称在模板中使用它们。 在这种情况下,我们称为块content :

{% block content %}{% endblock %}

Blocks are a way to render content from another template based on what we pass from the controller. Later on, we’ll go through how to pass the actual content we’re rendering here.

块是一种基于我们从控制器传递的内容来呈现另一个模板中的内容的方法。 稍后,我们将介绍如何传递此处呈现的实际内容。

As I’ve mentioned earlier, we’re using Twitter Bootstrap so we need to place it in the assets/lib/bootstrap directory. You can use the themes from bootswatch.com if you don’t want to use the default theme. Personally, I’m using the yeti theme. We also need jQuery as Twitter Bootstrap’s JavaScript depends on it, and we need it for handling events. We also need Handlebars for templating, and dropzone for uploading product images.

如前所述,我们正在使用Twitter Bootstrap,因此我们需要将其放置在assets/lib/bootstrap目录中。 如果您不想使用默认主题,则可以使用bootswatch.com中的主题。 就个人而言,我使用的是雪人主题。 我们还需要jQuery,因为Twitter BootstrapJavaScript依赖于jQuery,因此我们需要它来处理事件。 我们还需要把手的模板,并悬浮窗上载产品图片。

Notice that we have the following conditionals:

请注意,我们具有以下条件:

{% if is_productpage %} {% endif %}

We’re using it to only load the styles and scripts that are necessary for the current page.

我们使用它来仅加载当前页面所需的样式和脚本。

Go ahead and download the necessary files if you haven’t done so.

如果尚未下载所需文件,请继续。

Once you’re back, create the Home controller under the controllers directory. This will extend SlimController:

返回后,在controllers目录下创建Home控制器。 这将扩展SlimController :

<?php class Home extends \SlimController\SlimController { }

Inside the controller, declare the indexAction method. It uses the render method to render the index template for us. The render method accepts two arguments: the name of the template and some optional data that you want to pass. In this case we’re simply passing in a name:

在控制器内部,声明indexAction方法。 它使用render方法为我们渲染索引模板。 render方法接受两个参数:模板的名称和要传递的一些可选数据。 在这种情况下,我们只是传递一个名称:

public function indexAction(){ $this->render('index', array('title' => 'Home', 'name' => 'Dog')); }

Next, create the index template, name it index.twig. When using templates, you need to remember to give the template file a name that is the same as the one you used in the controller. From the index template, we need to call the extends method and pass the file name of the base template that we used. Next, we wrap in the content that we want to render in block tags. Make sure that the name you pass along is the same as the one you passed in the base template:

接下来,创建索引模板,将其命名为index.twig 。 使用模板时,需要记住给模板文件起一个名称,该名称与控制器中使用的名称相同。 从索引模板,我们需要调用extends方法,并传递我们使用的基本模板的文件名。 接下来,我们将要呈现的内容包装在block标签中。 确保您传递的名称与您在基本模板中传递的名称相同:

{% extends "base.twig" %} {% block content %} {{ parent() }} <h1>Hi {{ name }}</h1> {% endblock %}

Since we’re already writing the code for the home controller, we might as well write the method for acquiring the session id and user token. First let’s do the session ID. This will call the getSessionID() method from the Ebay class and return a session ID which we then store into the session. Then, we redirect the user to the eBay sign in page. Here we’re redirecting to the sandbox sign in page. I’ve also included the URL for the live one for those who want to deploy an eBay app later on. Both live and the sandbox version require the RuName and SessID to be passed along as a query parameter.

由于我们已经为家庭控制器编写了代码,因此我们最好编写获取会话ID和用户令牌的方法。 首先,让我们执行会话ID。 这将从Ebay类调用getSessionID()方法,并返回一个会话ID,然后将其存储到会话中。 然后,我们将用户重定向到eBay登录页面。 在这里,我们将重定向到沙盒登录页面。 我还为那些以后打算部署eBay应用程序的人提供了实时URL。 实时版本和沙盒版本都需要将RuName和SessID作为查询参数传递。

public function sessionAction(){ $session_id = $this->app->ebay->getSessionID(); $_SESSION['session_id'] = $session_id; //live: https://signin.ebay.co.uk/ws/eBayISAPI.dll?SignIn&RuName=" . $this->app->ebay->run_name . "&SessID=$session_id $redirect_url = "https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?SignIn&RuName=" . $this->app->ebay->run_name . "&SessID=" . $session_id; $this->app->redirect($redirect_url); }

Once the user has approved the app, the tokenAction() method will then be called. What this method does is retrieve the session id that we have saved earlier into the session. Then we use it as an argument for the getUserToken() method which returns the user token. We then save this user token into the database so we can use it later on when making requests to the API.

一旦用户批准了该应用程序,便会调用tokenAction()方法。 该方法的作用是检索我们先前保存到会话中的会话ID。 然后,将其用作getUserToken()方法的参数,该方法返回用户令牌。 然后,我们将该用户令牌保存到数据库中,以便稍后在向API发出请求时可以使用它。

public function tokenAction(){ $session_id = $_SESSION['session_id']; $token = $this->app->ebay->getUserToken($session_id); $id = 1; $settings_result = $this->app->db->query("SELECT user_token FROM settings WHERE id = $id"); $settings = $settings_result->fetch_object(); if(!empty($settings)){ $this->app->db->query("UPDATE settings SET user_token = '$token' WHERE id = $id"); }else{ $this->app->db->query("INSERT INTO settings SET user_token = '$token'"); } $this->app->redirect('/tester/ebay_trading_api'); }

路线 (Routes)

Next, we need to add the routes that we will be using throughout the app. We can do that from the index.php file right before the $app->run() call:

接下来,我们需要添加将在整个应用程序中使用的路由。 我们可以在$app->run()调用之前从index.php文件中$app->run() :

$app->addRoutes(array( '/' => 'Home:index', '/settings' => 'Settings:view', '/settings/update' => 'Settings:update', '/products/new' => 'Product:new', '/products/create' => 'Product:create', '/upload' => 'Product:upload', '/session' => 'Home:session', '/token' => 'Home:token', '/categories' => 'Product:categories' ));

In the code above, we’re using the addRoutes method from the Slim Controller extension. This takes up an array of routes, with the key being the actual route and the value being the controller and method. The controller and the method are separated by a colon :. The convention used by Slim is that every method in the controller which responds to a route should have a suffix of Action.

在上面的代码中,我们使用了Slim Controller扩展中的addRoutes方法。 这占用了一组路由,键是实际路由,值是控制器和方法。 控制器和方法用冒号分隔: 。 Slim使用的约定是,控制器中响应路由的每个方法都应具有Action后缀。

Here’s an overview of what each route does:

以下是每种路线的作用概述:

/ – home page.

/ –主页。

/settings – for viewing the store settings page.

/settings用于查看商店设置页面。

/setttings/update – for updating the store settings.

/setttings/update用于更新商店设置。

/products/new – for viewing the form for creating new products.

/products/new用于查看用于创建新产品的表单。

/products/create – for creating a new product.

/products/create用于创建新产品。

/upload – for uploading product images.

/upload –用于上传产品图片。

/session – for acquiring a session ID.

/session –用于获取会话ID。

/token – for acquiring a user token.

/token –用于获取用户令牌。

/categories – for suggesting product categories.

/categories –用于建议产品类别。

商店设定 (Store Settings)

Next, create a new file under the controllers directory and name it Settings.php. This will handle the updating of store settings. To cut this short a little bit, we will just enter some default data for the store settings so we won’t need to write the code for the creation of store settings. Execute the following query to insert the default data:

接下来,在controllers目录下创建一个新文件,并将其命名为Settings.php 。 这将处理商店设置的更新。 为了简化一点,我们将只为商店设置输入一些默认数据,因此我们无需编写用于创建商店设置的代码。 执行以下查询以插入默认数据:

INSERT INTO `store_settings` (`id`, `store_name`, `county`, `street`, `country_code_type`, `ebay_website`, `postal_code`, `category_mapping`, `category_prefill`, `currency_code`, `item_location`, `dispatch_time`, `listing_duration`, `listing_type`, `condition_type`, `optimal_picturesize`, `out_of_stock_control`, `get_it_fast`, `include_prefilled`, `shipping_profile`, `return_profile`, `payment_profile`, `shipping_service`, `shippingservice_priority`, `shippingservice_cost`, `shippingservice_additionalcost`) VALUES (1, 'My Awesome Store', 'Driftveil', '275 Star Street', 'GB', 'UK', 'XYZ 123', 1, 1, 'GBP', 'Driftveil Unova', 1, 'GTC', 'FixedPriceItem', '', 1, 1, 1, 1, '', '', '', '', 3, 30, 15);

Note that for this tutorial, we’re mainly going to use the first row in the table, the one which has an ID of 1 so we’re going to use 1 as the ID for fetching or updating the store settings table.

请注意,在本教程中,我们主要将使用表中的第一行,该行的ID为1,因此我们将使用1作为ID来获取或更新商店设置表。

Next, create a viewAction method and put in the following code:

接下来,创建一个viewAction方法并放入以下代码:

public function viewAction(){ $user_preferences = $this->app->ebay->getUserPreferences(); $shipping_services = $this->app->ebay->getEbayDetails('ShippingServiceDetails'); $condition_types_result = $this->app->db->query('SELECT id, name FROM condition_types'); $condition_types = array(); while($row = $condition_types_result->fetch_assoc()){ $condition_types[] = $row; } $listing_durations_result = $this->app->db->query('SELECT name FROM listing_durations'); $listing_durations = array(); while($row = $listing_durations_result->fetch_assoc()){ $listing_durations[] = $row['name']; } $listing_types_result = $this->app->db->query('SELECT name FROM listing_types'); $listing_types = array(); while($row = $listing_types_result->fetch_assoc()){ $listing_types[] = $row['name']; } $shippingservice_priorities = range(1, 4); $store_id = 1; $store_result = $this->app->db->query("SELECT store_name, county, street, country_code_type, ebay_website, postal_code, category_mapping, category_prefill, currency_code, item_location, dispatch_time, listing_duration, listing_type, condition_type, optimal_picturesize, out_of_stock_control, get_it_fast, include_prefilled, shipping_profile, return_profile, payment_profile, shipping_service, shippingservice_priority, shippingservice_cost, shippingservice_additionalcost FROM store_settings WHERE id = '$store_id'"); $store = $store_result->fetch_object(); $settings = array( 'user_preferences' => $user_preferences, 'shipping_services' => $shipping_services, 'condition_types' => $condition_types, 'listing_durations' => $listing_durations, 'listing_types' => $listing_types, 'store' => $store, 'shippingservice_priorities' => $shippingservice_priorities ); $this->render('settings/view', $settings); }

The viewAction method calls the getUserPreferences method from the Ebay class. We will go through that method later, but for now know that what it does is to return the seller profiles of the authenticated user. We’re also calling the getEbayDetails method which returns the shipping services available for the specific eBay website. Next, we fetch the condition types, listing durations and listing types from the database and assign those to an array. We then generate an array containing numbers 1 to 4. This will serve as the selection for shipping service priorities. Lastly, we fetch the store settings and render the page.

viewAction方法从Ebay类调用getUserPreferences方法。 我们稍后将介绍该方法,但现在知道它的作用是返回已认证用户的卖方资料。 我们还调用getEbayDetails方法,该方法返回可用于特定eBay网站的运输服务。 接下来,我们从数据库中获取条件类型 , 列出持续时间和列出类型 ,并将其分配给数组。 然后,我们生成一个包含数字1到4的数组。它将用作运输服务优先级的选择。 最后,我们获取商店设置并呈现页面。

Earlier, we’ve used 2 methods from the Ebay class but we haven’t really defined them yet. Open up Ebay.php in your classes directory and add the following methods.

之前,我们使用了Ebay类中的2个方法,但实际上还没有定义它们。 在您的classes目录中打开Ebay.php并添加以下方法。

First is the getUserPreferences method, which uses the GetUserPreferences method from the API. This allows you to fetch the user settings from the authenticated eBay account. This API method mostly needs boolean values. It returns a bunch of data so we’re filtering others out by specifying only the fields that we need. Here’s a breakdown of the fields we’re using:

首先是getUserPreferences方法,该方法使用API​​中的GetUserPreferences方法。 这使您可以从经过身份验证的eBay帐户中获取用户设置。 此API方法主要需要布尔值。 它返回了一堆数据,因此我们仅通过指定所需的字段来过滤掉其他数据。 以下是我们使用的字段的细分:

ShowGlobalShippingProgramListingPreference – if specified, it returns the data on whether the global shipping program is enabled or not.

ShowGlobalShippingProgramListingPreference-如果指定,它将返回有关是否启用了全球运输程序的数据。

ShowSellerExcludeShipToLocationPreference – if specified, it returns the excluded shipping locations.

ShowSellerExcludeShipToLocationPreference –如果指定,则返回排除的运送位置。

ShowSellerPaymentPreferences – if specified, it returns the payment preferences of the seller.

ShowSellerPaymentPreferences –如果指定,则返回卖方的付款偏好。

ShowSellerProfilePreferences – if specified, it returns the business policy information.

ShowSellerProfilePreferences –如果指定,则返回业务策略信息。

ShowSellerReturnPreferences – if specified, it returns the return preferences.

ShowSellerReturnPreferences –如果指定,则返回返回首选项。

For the seller profiles, we’re only getting the default ones, and then store them in an array with only the ID, type and name of the profile.

对于卖方资料,我们仅获取默认资料,然后将其存储在仅包含资料的ID,类型和名称的数组中。

public function getUserPreferences(){ $request_body = '<?xml version="1.0" encoding="utf-8"?> <GetUserPreferencesRequest xmlns="urn:ebay:apis:eBLBaseComponents"> <RequesterCredentials> <eBayAuthToken>' . $this->user_token . '</eBayAuthToken> </RequesterCredentials> <ShowGlobalShippingProgramListingPreference>true</ShowGlobalShippingProgramListingPreference> <ShowSellerExcludeShipToLocationPreference>true</ShowSellerExcludeShipToLocationPreference> <ShowSellerPaymentPreferences>true</ShowSellerPaymentPreferences> <ShowSellerProfilePreferences>true</ShowSellerProfilePreferences> <ShowSellerReturnPreferences>true</ShowSellerReturnPreferences> </GetUserPreferencesRequest>'; $response = $this->request('GetUserPreferences', $request_body); $seller_profiles = array(); foreach($response->SellerProfilePreferences->SupportedSellerProfiles->SupportedSellerProfile as $profile){ $is_default = $profile->CategoryGroup->IsDefault; if($is_default == 'true'){ $seller_profiles[] = array( 'id' => json_decode(json_encode($profile->ProfileID), true)[0], 'type' => json_decode(json_encode($profile->ProfileType), true)[0], 'name' => json_decode(json_encode($profile->ProfileName), true)[0] ); } } $user_preferences = array( 'seller_profiles' => $seller_profiles, 'paypal_emailaddress' => json_decode(json_encode($response->SellerPaymentPreferences->DefaultPayPalEmailAddress), true)[0] ); return $user_preferences; }

The other method that we used is getEbayDetails. It uses the GeteBayDetails method from the API. We only need to specify a value for DetailName for this to work. Earlier, we supplied ShippingServiceDetails as the value. This allows us to get a list of shipping services supported by the eBay website where the product will be listed. For a list of other applicable values for the DetailName, you can check out this page.

我们使用的另一种方法是getEbayDetails 。 它使用API​​中的GeteBayDetails方法。 我们只需要为DetailName指定一个值即可。 之前,我们提供了ShippingServiceDetails作为值。 这使我们可以获得列出该产品的eBay网站支持的运输服务列表。 有关DetailName其他适用值的DetailName ,您可以签出此页面 。

public function getEbayDetails($detail_name){ $request_body = '<?xml version="1.0" encoding="utf-8"?> <GeteBayDetailsRequest xmlns="urn:ebay:apis:eBLBaseComponents"> <RequesterCredentials> <eBayAuthToken>' . $this->user_token . '</eBayAuthToken> </RequesterCredentials> <DetailName>' . $detail_name . '</DetailName> </GeteBayDetailsRequest>'; $response = $this->request('GeteBayDetails', $request_body); $services = array(); foreach($response->ShippingServiceDetails as $service){ $services[] = json_decode(json_encode($service->ShippingService), true)[0]; } return $services; }

Going back to the settings controller, add the updateAction method. What this does is update the store settings. Here we’re using valitron to do the validation. If the validation passes, we extract the values one by one. For the checkboxes, we check if the corresponding field is not empty. If it’s not then we set the value to 1, otherwise 0. After that we use a prepared statement to update the store settings, pass in a message to be returned to the user, then redirect to the settings page. If the validation fails, we simply pass in the errors and redirect to the settings page.

回到设置控制器,添加updateAction方法。 这样做是更新商店设置。 在这里,我们使用valitron进行验证。 如果验证通过,我们将一一提取值。 对于复选框,我们检查相应的字段是否不为空。 如果不是,则将值设置为1,否则设置为0。此后,我们使用准备好的语句更新商店设置,传递一条消息以返回给用户,然后重定向到设置页面。 如果验证失败,我们只需传递错误并重定向到设置页面。

public function updateAction(){ $v = new Valitron\Validator($_POST); $v->rule('required', array('store_name', 'county', 'street', 'country_code_type', 'ebay_website', 'postal_code', 'currency_code', 'item_location', 'dispatch_time', 'listing_duration', 'listing_type', 'condition_type', 'PAYMENT', 'RETURN_POLICY', 'SHIPPING', 'shipping_service', 'shippingservice_priority', 'shippingservice_cost', 'shippingservice_additionalcost')); if($v->validate()){ $id = 1; //store settings id $store_name = $_POST['store_name']; $street = $_POST['street']; $county = $_POST['county']; $country_code_type = $_POST['country_code_type']; $ebay_website = $_POST['ebay_website']; $postal_code = $_POST['postal_code']; $category_mapping = (!empty($_POST['category_mapping'])) ? 1 : 0; $category_prefill = (!empty($_POST['category_prefill'])) ? 1 : 0; $optimal_picturesize = (!empty($_POST['optimal_picturesize'])) ? 1 : 0; $out_of_stock_control = (!empty($_POST['out_of_stock_control'])) ? 1 : 0; $get_it_fast = (!empty($_POST['get_it_fast'])) ? 1 : 0; $include_prefilled = (!empty($_POST['include_prefilled'])) ? 1 : 0; $currency_code = $_POST['currency_code']; $item_location = $_POST['item_location']; $dispatch_time = $_POST['dispatch_time']; $listing_duration = $_POST['listing_duration']; $listing_type = $_POST['listing_type']; $condition_type = $_POST['condition_type']; $payment_policy = $_POST['PAYMENT']; $return_policy = $_POST['RETURN_POLICY']; $shipping_policy = $_POST['SHIPPING']; $shipping_service = $_POST['shipping_service']; $shippingservice_priority = $_POST['shippingservice_priority']; $shippingservice_cost = $_POST['shippingservice_cost']; $shippingservice_additionalcost = $_POST['shippingservice_additionalcost']; if($query = $this->app->db->prepare("UPDATE store_settings SET store_name = ?, county = ?, street = ?, country_code_type = ?, ebay_website = ?, postal_code = ?, category_mapping = ?, category_prefill = ?, currency_code = ?, item_location = ?, dispatch_time = ?, listing_duration = ?, listing_type = ?, condition_type = ?, optimal_picturesize = ?, out_of_stock_control = ?, get_it_fast = ?, include_prefilled = ?, shipping_profile = ?, return_profile = ?, payment_profile = ?, shipping_service = ?, shippingservice_priority = ?, shippingservice_cost = ?, shippingservice_additionalcost = ? WHERE id = ?")){ $query->bind_param("ssssssiississsiiiissssiddi", $store_name, $county, $street, $country_code_type, $ebay_website, $postal_code, $category_mapping, $category_prefill, $currency_code, $item_location, $dispatch_time, $listing_duration, $listing_type, $condition_type, $optimal_picturesize, $out_of_stock_control, $get_it_fast, $include_prefilled, $shipping_policy, $return_policy, $payment_policy, $shipping_service, $shippingservice_priority, $shippingservice_cost, $shippingservice_additionalcost, $id); $query->execute(); $this->app->flash('message', array('type' => 'success', 'text' => 'Settings was updated!')); $this->app->redirect('/tester/ebay_trading_api/settings'); } }else{ $this->app->flash('form', $_POST); $this->app->flash('message', array( 'type' => 'danger', 'text' => 'Please fix the following errors', 'data' => $v->errors()) ); $this->app->redirect('/tester/ebay_trading_api/settings'); } }

结论 (Conclusion)

In this part, we implemented a basic updating mechanism for our store’s settings. In the next and final part of this series, we’ll be adding the Product functionality – the ability to add new products to our eBay store. Stay tuned!

在这一部分中,我们实现了商店设置的基本更新机制。 在本系列的下一个也是最后一部分,我们将添加产品功能-可以向我们的eBay商店添加新产品的功能。 敬请关注!

翻译自: https://www.sitepoint.com/configuring-stores-settings-ebay-trading-api/

ebay api

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)