2016-02-25 149 views
0

所以我似乎無法得到此代碼的工作。我在Finding API下使用了一個非常類似的代碼,它很好用,爲交易API做了適當的更改(端點,標題等),現在它不起作用。在使用eBay API測試工具時,一切似乎都很好,所以我不確定問題可能出在哪裏。易趣交易API PHP XML呼叫失敗(Ack失敗)

任何人都可以提供見解? ack返回失敗而不是成功。我的PHP啓用了curl,這也不是問題。先謝謝你!

<?php 
error_reporting(E_ALL); // Turn on all errors, warnings, and notices for easier debugging 

// API request variables 
$endpoint = 'https://api.ebay.com/ws/api.dll'; // URL to call 
$query = 'username';     // Supply your own query keywords as needed 

// Construct the findItemsByKeywords POST call 
$resp = simplexml_load_string(constructPostCallAndGetResponse($endpoint, $query)); 

// Check to see if the call was successful, else print an error 
if ($resp->ack == "Success") { 
    $results = ''; // Initialize the $results variable 

    // Parse the desired information from the response 
    foreach($resp->searchResult->item as $item) { 
    $pic = $item->galleryURL; 
    $link = $item->viewItemURL; 
    $price = $item->sellingStatus->currentPrice;  
    $title = $item->title; 

    // Build the desired HTML code for each searchResult.item node and append it to $results 
    $results .= "<tr><td><img src=\"$pic\"></td><td>$price</td><td><a href=\"$link\">$title</a></td></tr>"; 
    } 
} 
else { // If the response does not indicate 'Success,' print an error 
    $results = "<h3>Oops! The request was not successful."; 
} 

?> 

<!-- Build the HTML page with values from the call response --> 
<html> 
<head> 
<title>eBay Search Results for <?php echo $query; ?></title> 
<style type="text/css">body { font-family: arial,sans-serif;} </style> 
</head> 
<body> 

<h1>eBay Search Results for <?php echo $query; ?></h1> 

<table> 
<tr> 
    <td> 
    <?php echo $results;?> 
    </td> 
</tr> 
</table> 

</body> 
</html> 

<?php 

function constructPostCallAndGetResponse($endpoint, $query) { 
    global $xmlrequest; 

    // Create the XML request to be POSTed 
    $xmlrequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; 
    $xmlrequest .= "<GetBidderListRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">\n"; 
    $xmlrequest .= "<RequesterCredentials>\n <eBayAuthToken>xxxxxxxxxxx</eBayAuthToken></RequesterCredentials>\n"; 
    $xmlrequest .= "<UserID>"; 
    $xmlrequest .= $query; 
    $xmlrequest .= "</UserID>\n"; 
    $xmlrequest .= "</GetBidderListRequest>"; 

    // Set up the HTTP headers 
    $headers = array(
    'X-EBAY-API-COMPATIBILITY-LEVEL:951', 
    'X-EBAY-API-DEV-NAME:xxxxxxxxxx', 
    'X-EBAY-API-APP-NAME:xxxxxxxxxx', 
    'X-EBAY-API-CERT-NAME:xxxxxxxxxx', 
    'X-EBAY-API-SITEID:0', 
    'X-EBAY-API-CALL-NAME:GetBidderList' 
); 

    $session = curl_init($endpoint);      // create a curl session 
    curl_setopt($session, CURLOPT_POST, true);    // POST request type 
    curl_setopt($session, CURLOPT_HTTPHEADER, $headers); // set headers using $headers array 
    curl_setopt($session, CURLOPT_POSTFIELDS, $xmlrequest); // set the body of the POST 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // return values as a string, not to std out 

    $responsexml = curl_exec($session);      // send the request 
    curl_close($session);         // close the session 
    return $responsexml;         // returns a string 

} // End of constructPostCallAndGetResponse function 

?> 

回答

0

故障在行if ($resp->ack == "Success") {。正確的字段名稱是Ack。 Trading API中的字段與Finding API中的字段不同。將代碼更改爲if ($resp->Ack == "Success") {應該可以工作。

+0

我嘗試了大寫和小寫確認和確認,沒有什麼區別。 – Jonebone

+0

您可以使用以下內容查看eBay在回覆中返回的錯誤。 (錯誤:%s \ n「,$ error-> ShortMessage); } –

+0

試過,沒有運氣。即使啓用了curl,代碼似乎仍然在curl部分失敗。我有另一個人看着它,他也弄不清楚。目前,我的解決方法是在eBay上運行API工具,然後將輸出複製爲.xml文件並運行此代碼的上半部分以生成輸出。這是一個更多的手冊,但它至少能夠讓我從API尋求的數據。 – Jonebone