【发布时间】:2016-02-25 16:20:22
【问题描述】:
所以我似乎无法让这段代码正常工作。我在 Find API 下使用了一个非常相似的代码,效果很好,对 Trading API 进行了适当的更改(端点、标题等),但现在它不起作用了。在使用 eBay API 测试工具时,一切似乎都很好,所以我不确定问题可能是什么。
谁能提供见解? 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
?>
【问题讨论】: