【问题标题】:eBay Trading API PHP XML call fails (Ack failure)eBay Trading API PHP XML 调用失败(确认失败)
【发布时间】: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

?>

【问题讨论】:

    标签: php xml ebay-api


    【解决方案1】:

    故障在if ($resp-&gt;ack == "Success") { 行中。正确的字段名称是Ack。交易 API 中的字段与查找 API 中的字段不同。将代码更改为 if ($resp-&gt;Ack == "Success") { 应该可以工作。

    【讨论】:

    • 我尝试了大写和小写的ack和ack,没有任何区别。
    • 您可以使用以下命令查看 eBay 在响应中返回的错误。 foreach ($resp->Errors as $error) { printf("Error: %s\n", $error->ShortMessage); }
    • 试过了,也没有运气。尽管启用了 curl,但 curl 部分的代码似乎失败了。我让另一个人看着它,他也无法弄清楚。目前,我的解决方法是在 eBay 上运行 API 工具,然后将输出复制为 .xml 文件并运行此代码的上半部分以生成输出。它有点手动,但它至少能够让我从 API 中获取我正在寻找的数据。
    猜你喜欢
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2014-02-15
    • 2021-06-12
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多