易趣开发者改进

Ebay developer Improvements

经过大量的研究和实践,我一直在尝试获取所有当前有效产品的列表,我设法整理了一个脚本,该脚本将列出我需要的信息,即可用库存和 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)

这可用于获取您在 eBay 上的所有活动列表。解释这基本上是以 xml 的形式向 eBay 提出请求。 xml 请求将告诉 eBay 您想要什么,然后 eBay 将以适当的信息(在这种情况下是您 eBay 帐户上的有效列表)做出响应。您的 xml 响应存储在 var $response.

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;

/**
**Change your call name to the appropriate call you need to make so for example to get list of all active products on your ebay you can use GetMyEbaySelling
**
**/

            $call_name = 'GetMyeBaySelling';

            // 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,
            );

/**
**XML request is what your telling ebay you need so this is the basic request to get hold of the active listings
**just to note the listings would be in a var called response.
**/

                $xml_request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
                            <GetMyeBaySellingRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">
                            <RequesterCredentials>
                            <eBayAuthToken>". $auth_token . "</eBayAuthToken>
                            </RequesterCredentials>
                            <Version>753</Version>
                            <ActiveList>
                            <Sort>TimeLeft</Sort>
                            <Pagination>
                            <EntriesPerPage>100</EntriesPerPage>
                            <PageNumber>1</PageNumber>
                            </Pagination>
                            </ActiveList>
                            </GetMyeBaySellingRequest>";

            // 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);

这里有说明 http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/GetMyeBaySelling.html