php - 易趣开发者改进

标签 php mysql xml api ebay-api

在大量研究和实践之后,我一直试图获得所有当前活跃产品的列表,我设法整理了一个脚本,该脚本将列出我需要的信息,即可用库存和 sku。现在我遇到的问题是我必须从网站上手动输入 ebay id,但是我需要的是自动提取活跃的 ebay 列表。

下面是收集单个产品信息的脚本。

error_reporting(E_ALL);
    ini_set('display_errors', '1');

    // Load configuration file
    $sandbox = false;
    $compat_level = 753;
    $api_endpoint = $sandbox ? 'https://api.sandbox.ebay.com/ws/api.dll' : 'https://api.ebay.com/ws/api.dll';
    $dev_id = $sandbox ? " " : " ";
    $app_id = $sandbox ? " " : " ";
    $cert_id = $sandbox ? " " : " ";
    $auth_token = $sandbox ? " " : " ";


    $site_id = 3;
    $call_name = 'GetItem';

    // Create headers to send with CURL request.
    $headers = array 
    (
        'X-EBAY-API-COMPATIBILITY-LEVEL: ' . $compat_level,
        '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: ' . $call_name,          
        'X-EBAY-API-SITEID: ' . $site_id,
    );

    // Generate XML request
    $xml_request = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
                   <".$call_name."Request xmlns=\"urn:ebay:apis:eBLBaseComponents\">
                       <RequesterCredentials>
                           <eBayAuthToken>" . $auth_token . "</eBayAuthToken>
                       </RequesterCredentials>
                       <DetailLevel>ReturnAll</DetailLevel>
                       <IncludeItemSpecifics>true</IncludeItemSpecifics>
                       <IncludeWatchCount>true</IncludeWatchCount>
                       <ItemID>191744777908</ItemID>
                   </".$call_name."Request>";

    // Send request to eBay and load response in $response
    $connection = curl_init();
    curl_setopt($connection, CURLOPT_URL, $api_endpoint);
    curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($connection, CURLOPT_POST, 1);
    curl_setopt($connection, CURLOPT_POSTFIELDS, $xml_request);
    curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($connection);
    curl_close($connection);

    // Create DOM object and load eBay response
    $dom = new DOMDocument();
    $dom->loadXML($response);

    // Parse data accordingly.
    $ack = $dom->getElementsByTagName('Ack')->length > 0 ? $dom->getElementsByTagName('Ack')->item(0)->nodeValue : '';
    $eBay_official_time = $dom->getElementsByTagName('Timestamp')->length > 0 ? $dom->getElementsByTagName('Timestamp')->item(0)->nodeValue : '';
    //$current_price = $dom->getElementsByTagName('ConvertedCurrentPrice')->length > 0 ? $dom->getElementsByTagName('ConvertedCurrentPrice')->item(0)->nodeValue : '';
    //$description = $dom->getElementsByTagName('Description')->length > 0 ? $dom->getElementsByTagName('Description')->item(0)->nodeValue : '';
    $itemID = $dom->getElementsByTagName('ItemID')->length > 0 ? $dom->getElementsByTagName('ItemID')->item(0)->nodeValue : '';
    $customLabel = $dom->getElementsByTagName('SKU')->length > 0 ? $dom->getElementsByTagName('SKU')->item(0)->nodeValue : '';
    $quantity = $dom->getElementsByTagName('Quantity')->length > 0 ? $dom->getElementsByTagName('Quantity')->item(0)->nodeValue : '';
    $quantitySold = $dom->getElementsByTagName('QuantitySold')->length > 0 ? $dom->getElementsByTagName('QuantitySold')->item(0)->nodeValue : '';
    $quantityAvaliable = $quantity  - $quantitySold;

    echo "ItemID: ".$itemID."<br>";
    echo "sku: ".$customLabel."<br>";
    echo "quantity: ".$quantity."quantitySold: ".$quantitySold." quantityAvaliable:".$quantityAvaliable;

我想不通的是如何自动从我的 eBay 商店获取列表。而不必单独输入每个项目 ID。

最佳答案

您正在使用提取单个项目的 Getitem API,您需要更改对 GetMyeBaySelling 的调用

$xml_request = '<?xml version="1.0" encoding="utf-8" ?>';
$xml_request .= '<GetMyeBaySellingRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
$xml_request .= '<ActiveList>';
$xml_request .= '<Sort>TimeLeft</Sort>';
$xml_request .= '<Pagination><EntriesPerPage>200</EntriesPerPage>';
$xml_request .= "<PageNumber>1</PageNumber>";
$xml_request .= '</Pagination>';
$xml_request .= '</ActiveList>';
$xml_request .= "<RequesterCredentials><eBayAuthToken>$auth_token</eBayAuthToken></RequesterCredentials>";
$xml_request .= '<WarningLevel>High</WarningLevel>';
$xml_request .= '</GetMyeBaySellingRequest>​';

注意 1:这对您来说只是一个开始,这将使您获得商店中商品的第 1 页,您可以将此处的 1 设为变量以遍历页面 "<PageNumber>1</PageNumber>";

您可能会问,您怎么知道您有多少页?答案是你不需要知道,eBay会在你下载第一个的时候给你页数。您要查找的节点是 ActiveList->PaginationResult->TotalNumberOfPages然后使用类似这样的方式遍历页面 while ($PageNumber < $TotalNumberOfPages) {

注意 2:如果要匹配 </".$call_name."Request>";,您可能还想将上面的代码改回变量。就像你以前一样。但是你需要改变$call_name = 'GetItem';$call_name = 'GetMyeBaySelling';

注意 3:您的站点 ID 是 $site_id = 3; ,如果你在美国,你可能想把它改成 1

注意 4:您可能还想更改 $compat_level = 753;到最新版本 951 trading api version page

注意 5:由于看起来您只是在使用 eBay API,我不妨告诉您您正在使用交易 API,如果您决定使用不同的 API,例如商品 API,您将需要更改终点,但您现在拥有的终点已正确设置为 getmyebayselling ( endpoint docs are here )

关于php - 易趣开发者改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35059340/

相关文章:

android - 从另一个 xml 元素引用 XML 属性

c# - 如何防止任何人向我的网络服务发送请求

php - 调用数据库返回不完整的数据

PHPStorm 缺少返回语句

php - 如何在 PHP 中声明一个可以跨模板使用的全局变量?

mysql - 如何编写条件 mysql 查询?

php - 如何从这个 xml 在 php 中生成 soap 请求?

php - 合并请求不起作用

mysql - 获取续集生成的sql以生成不带反引号的字段名称

php - 如何再获取 1 但稍后再删除它?