Ebay Trading API PHP XML调用失败(确认失败)

时间:2016-02-25 16:20:22

标签: php xml api ebay

所以我似乎无法使这段代码正常工作。我在Finding 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

?>

1 个答案:

答案 0 :(得分:0)

错误在if ($resp->ack == "Success") {行。正确的字段名称为Ack。 Trading API中的字段与Finding API中的字段不同。将代码更改为if ($resp->Ack == "Success") {应该有效。