2016-04-13 16 views
1

初めての投稿に長い時間がかかる、私は困惑している!XML APIを使用してeBayでカテゴリを引き出す

私は雇用者のeBayショップでダイナミックなカテゴリリストを取得しようとしていますが、回答や事前に書かれたコードを辿るのに多くの時間を費やしていますが、私はそれを自分でやらなければならないだろう。

eBay Dev APIのテスト場で遊んで、私が望むカテゴリを引き下げました。私が見つけにくいのは、結果をスタイル付けして整理する方法です。

これは、XMLで出力されるタグを対象とすることはできますが、標準ではないため、私はできません。

これは私がこれまでに書かれたもので、「風の色:赤は」:

<?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> 
div要素が働いていたし、すべての必要な資格情報が「keys.php」ファイルに格納されているテストするだけでした

上記のコードの大部分は、資格情報のチェックとエラー管理です。実際には、重い作業を行うコードの最後の12行ほどです。

現在のところ、すべてのカテゴリとそれに対応する子カテゴリが1つの長い縦リストに出力されています。私は子供を字下げし、フォントサイズを小さくしたいと思うのが理想です。それらをDIVとCSSを適用することであったが、私はあなたが幸せであれば

は事前

答えて

1

で任意の助けてくれてありがとう、何か提案やコメントは、この時点では歓迎されている、もっと簡単な方法があると思いますし始めていますあなたのプロジェクトでComposerを使用するには、APIの使用を簡略化するSDK for PHPを開発しました。以下の例では、ストアカテゴリを正しい順序で取得し、ネストされた<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; 
+0

これは大変ありがとうございました! – RobinB

関連する問題