使用XML API在易趣上进行分类


Pulling in categories on eBay using XML API

第一次发帖潜伏了很长时间,我被难住了!

一段时间以来,我一直试图在我的雇主eBay商店上获得一个动态类别列表,我花了很多时间搜索答案或预先编写的代码,但得出的结论是,如果我想完成,我就必须自己完成(完全可以,但很耗时)。

在易趣开发人员API测试场上,我已经成功地找到了我想要的类别。我发现困难的是如何设计和组织结果。

如果我可以用CSS针对XML输出的标签,这会更容易,但由于它们是非标准的,我做不到。

这就是我到目前为止所写的,"style-color:red"只是为了测试div是否工作,所有所需的凭据都存储在"keys.php"文件中:

<?php
/*  © 2013 eBay Inc., All Rights Reserved */ 
/* Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php */
require_once('keys.php') ?>
<?php require_once('eBaySession.php') ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>GetCatagories</TITLE>
<style type="text/css">
#child {
    color:red;
}
</style>
</HEAD>
<BODY>
<?php    
    //SiteID must also be set in the Request's XML
    //SiteID = 3  (US) - UK = 3, Canada = 2, Australia = 15, ....
    //SiteID Indicates the eBay site to associate the call with
    $siteID = 3;
    //the call being made:
    $verb = 'GetStore';
    //Level / amount of data for the call to return (default = 0)
    $detailLevel = 0;

    ///Build the request Xml string
    $requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
    $requestXmlBody .= '<GetStoreRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
    $requestXmlBody .= "<RequesterCredentials><eBayAuthToken>$userToken</eBayAuthToken></RequesterCredentials>";
    $requestXmlBody .= '</GetStoreRequest>';
    //Create a new eBay session with all details pulled in from included keys.php
    $session = new eBaySession($userToken, $devID, $appID, $certID, $serverUrl, $compatabilityLevel, $siteID, $verb);
    //send the request and get response
    $responseXml = $session->sendHttpRequest($requestXmlBody);
    if(stristr($responseXml, 'HTTP 404') || $responseXml == '')
        die('<P>Error sending request');
    //Xml string is parsed and creates a DOM Document object
    $responseDoc = new DomDocument();
    $responseDoc->loadXML($responseXml);

    //get any error nodes
    $errors = $responseDoc->getElementsByTagName('Errors');
    //if there are error nodes
    if($errors->length > 0)
    {
        echo '<P><B>eBay returned the following error(s):</B>';
        //display each error
        //Get error code, ShortMesaage and LongMessage
        $code = $errors->item(0)->getElementsByTagName('ErrorCode');
        $shortMsg = $errors->item(0)->getElementsByTagName('ShortMessage');
        $longMsg = $errors->item(0)->getElementsByTagName('LongMessage');
        //Display code and shortmessage
        echo '<P>', $code->item(0)->nodeValue, ' : ', str_replace(">", "&gt;", str_replace("<", "&lt;", $shortMsg->item(0)->nodeValue));
        //if there is a long message (ie ErrorLevel=1), display it
        if(count($longMsg) > 0)
            echo '<BR>', str_replace(">", "&gt;", str_replace("<", "&lt;", $longMsg->item(0)->nodeValue));
    }
    else //no errors
    {
        $i = 2;
        while ($i <= 188) {
                echo '<div id="catagories">';
                echo $responseDoc->getElementsByTagName("Name")->item($i++)-     >textContent;
                echo '<BR>';
                echo '</div>';
            }
        }         
?>
</BODY>
</HTML>

上面的大部分代码都是凭证检查和错误管理——实际上是最后12行左右的代码完成了繁重的工作。

目前,它已经在一个长的垂直列表中输出了我的所有类别和相应的子类别,理想情况下,我想缩进子类别并给它们一个较小的字体大小,(在我看来)最简单的方法是将它们分开并应用CSS,但我开始认为有一种更简单的方法,在这一点上,欢迎任何建议或评论,

感谢您提前提供的任何帮助

如果您愿意在项目中使用Composer,我已经开发了一个用于PHP的SDK,可以简化API的使用。下面的例子将以正确的顺序获得商店类别,并使用嵌套的<ol>元素输出一个简单的树,这些元素可以是使用CSS的样式。

<?php
require __DIR__.'/vendor/autoload.php';
use 'DTS'eBaySDK'Constants;
use 'DTS'eBaySDK'Trading'Services;
use 'DTS'eBaySDK'Trading'Types;
use 'DTS'eBaySDK'Trading'Enums;
$service = new Services'TradingService([
    'credentials' => [
        'appId'  => $appID,
        'devId'  => $devID,
        'certId' => $certID
    ],
    'authToken'   => $userToken,
    'siteId'      => Constants'SiteIds::GB
]);
$request = new Types'GetStoreRequestType();
$request->CategoryStructureOnly = true;
$response = $service->getStore($request);
$html = '';
if (isset($response->Errors)) {
    $html .= '<p>eBay returned the following error(s):<br/>';
    foreach ($response->Errors as $error) {
        $html .= sprintf(
            "%s: %s<br/>%s<br/>",
            $error->SeverityCode === Enums'SeverityCodeType::C_ERROR ? 'Error' : 'Warning',
            htmlentities($error->ShortMessage),
            htmlentities($error->LongMessage)
        );
    }
    $html .= '</p>';
}
if ($response->Ack !== 'Failure') {
    $categories = iterator_to_array($response->Store->CustomCategories->CustomCategory);
    usort($categories, 'sortCategories');
    foreach ($categories as $category) {
        $html .= '<ol>'.buildCategory($category).'</ol>';
    }
}
function buildCategory($category)
{
    $html = '<li>';
    $html .= htmlentities($category->Name);
    $children = iterator_to_array($category->ChildCategory);
    usort($children, 'sortCategories');
    foreach ($children as $child) {
        $html .= '<ol>'.buildCategory($child).'</ol>';
    }
    $html .= '</li>';
    return $html;
}
function sortCategories($a, $b)
{
    if ($a->Order === $b->Order) {
        return 0;
    }
    return ($a->Order < $b->Order) ? -1 : 1;
}
echo <<< EOF_HTML
  <!doctype html>
  <html>
    <head>
      <meta charset="utf-8">
      <title>Example</title>
      <style>
        ol {
          list-style-type: none;
        }
      </style>
    </head>
    <body>
        $html
    </body>
  </html>
EOF_HTML;