Php 使用XML API在易趣上引入类别

Php 使用XML API在易趣上引入类别,php,html,css,xml,Php,Html,Css,Xml,长时间潜伏第一次发帖,我被难住了 我一直在尝试在我的雇主易趣店铺上获得一个动态分类列表,现在,我花了很多时间寻找答案或预先编写的代码,但得出的结论是,如果我想这样做,我必须自己去做(很好,但很耗时) 通过使用eBay开发API测试场,我成功地获得了我想要的类别。我发现困难的是如何设计和组织结果 如果我可以将XML输出的标记作为CSS的目标,这会更容易,但是由于它们是非标准的,所以我不能 这就是我到目前为止所写的,'style color:red'只是为了测试div是否正常工作,所有必需的凭据都存

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

我一直在尝试在我的雇主易趣店铺上获得一个动态分类列表,现在,我花了很多时间寻找答案或预先编写的代码,但得出的结论是,如果我想这样做,我必须自己去做(很好,但很耗时)

通过使用eBay开发API测试场,我成功地获得了我想要的类别。我发现困难的是如何设计和组织结果

如果我可以将XML输出的标记作为CSS的目标,这会更容易,但是由于它们是非标准的,所以我不能

这就是我到目前为止所写的,'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>

GetCategories
#孩子{
颜色:红色;
}
如果您愿意在您的项目中使用,我已经开发了一个简化API使用的工具。下面的示例将以正确的顺序获取商店类别,并使用嵌套的
元素输出一个简单的树,这些元素可以是使用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;