2016-02-25 116 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