如何用php解析ebay xml响应

时间:2016-08-18 19:37:07

标签: php xml

我正在尝试用ebay解析用于finditem api的XML响应。这是我的代码:

<head>
    <title>Title</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?

$pagenumber = 1;
$keyword = 'iphone';
$entriesperpage = 1;
$ebayusername = 'ebay-username-here';

$response = getFindProducts($keyword, $ebayusername, $entriesperpage);
echo '<div style="display: none;">' . $response . '</div>';

$xmlResponse = new SimpleXMLElement($response);
echo $xmlResponse -> searchresult -> item[0] -> title;

function getFindProducts($keyword,$ebayusername,$entriesperpage) {

$xmlRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlRequest .= '<findItemsAdvancedRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">';
$xmlRequest .= '<keywords>' . $keyword . '</keywords>';
$xmlRequest .= '<itemFilter>';
$xmlRequest .= '<name>Seller</name>';
$xmlRequest .= '<value>' . $ebayusername . '</value>';
$xmlRequest .= '</itemFilter>';
$xmlRequest .= '<paginationInput>';
$xmlRequest .= '<entriesPerPage>' . $entriesperpage . '</entriesPerPage>';
$xmlRequest .= '</paginationInput>';
$xmlRequest .= '</findItemsAdvancedRequest>';

// define our header array for the Shopping API call
$headers = array(
    'X-EBAY-SOA-SECURITY-APPNAME:' . 'appname-here',
    'X-EBAY-SOA-OPERATION-NAME:findItemsAdvanced'
);

// initialize our curl session
$session  = curl_init(FINDING_API_ENDPOINT);

// set our curl options with the XML request
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

if(curl_exec($session) === false){ echo 'Curl error: ' . curl_error($session); }

// execute the curl request
$responseXML = curl_exec($session);

// close the curl session
curl_close($session);

// return the response XML
return $responseXML;

}

?>
</body>

</html>

当我运行它时,它成功运行,因为它确实从ebay获得响应,如果我在源代码中回显整个页面,我可以看到产品XML结构。

问题出在以下部分:

$xmlResponse = new SimpleXMLElement($response);
echo $xmlResponse -> searchresult -> item[0] -> title;

它无声地失败而没有任何错误。我确实打开了错误报告。

非常感谢任何帮助!

---编辑---

XML代码响应的结构如下:

<!--?xml version='1.0' encoding='UTF-8'?-->
<finditemsadvancedresponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<ack>Success</ack>
<version>1.13.0</version>
<timestamp>2016-08-18T19:25:06.303Z</timestamp>
<searchresult count="1">
  <item>
    <itemid>123456789</itemid>
    <title>Item Title</title>
    <globalid>EBAY-US</globalid>
    <primarycategory>
      <categoryid>12345</categoryid>
      <categoryname>Category Name</categoryname>
    </primarycategory>
    <galleryurl>Galery URL</galleryurl>
    <viewitemurl>Item URL</viewitemurl>
    <paymentmethod>PayPal</paymentmethod>
    <autopay>false</autopay>
    <postalcode>12345</postalcode>
    <location>Location</location>
    <country>US</country>
    <shippinginfo>
      <shippingservicecost currencyid="USD">0.0</shippingservicecost>
      <shippingtype>Free</shippingtype>
      <shiptolocations>US</shiptolocations>
      <expeditedshipping>true</expeditedshipping>
      <onedayshippingavailable>false</onedayshippingavailable>
      <handlingtime>1</handlingtime>
    </shippinginfo>
    <sellingstatus>
      <currentprice currencyid="USD">15.99</currentprice>
      <convertedcurrentprice currencyid="USD">15.99</convertedcurrentprice>
      <sellingstate>Active</sellingstate>
      <timeleft>P9DT3H29M13S</timeleft></sellingstatus>
      <listinginfo>
        <bestofferenabled>false</bestofferenabled>
        <buyitnowavailable>false</buyitnowavailable>
        <starttime>2015-12-01T22:54:19.000Z</starttime>
        <endtime>2016-08-27T22:54:19.000Z</endtime>
        <listingtype>FixedPrice</listingtype>
        <gift>false</gift>
      </listinginfo>
      <returnsaccepted>true</returnsaccepted>
      <condition>
        <conditionid>3000</conditionid>
        <conditiondisplayname>Used</conditiondisplayname>
      </condition>
      <ismultivariationlisting>false</ismultivariationlisting>
      <topratedlisting>false</topratedlisting>
  </item>
</searchresult>
<paginationoutput>
  <pagenumber>1</pagenumber>
  <entriesperpage>1</entriesperpage>
  <totalpages>100</totalpages>
  <totalentries>100</totalentries>
</paginationoutput>
<itemsearchurl>http://www.ebay.com/sch/i.html?_sasl=allkillerdeals&amp;_saslop=1&amp;_fss=1&amp;LH_SpecificSeller=1&amp;_nkw=knife&amp;_ddo=1&amp;_ipg=1&amp;_pgn=1</itemsearchurl>
</finditemsadvancedresponse>

1 个答案:

答案 0 :(得分:0)

最后破解了它。经过几个小时的挫折后,终于发现xml回声路径 case sensative 。只是发生在实际的脚本中,因为当我从html源代码复制xml(对于上面的例子)时,它显示了所有小写。但实际返回的xml在xml节点中具有混合大小写。

所以,

    echo $xmlResponse -> searchresult -> item[0] -> title;

需要:

    echo $xmlResponse -> searchResult -> item[0] -> title;