TK421 TK421 - 3 months ago 32
PHP Question

How to parse ebay xml response with php

I am trying to parse XML response from ebay for the findingitem api with php. Here is my code:




<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>


When I run it, it runs successfully in that it does get the response from ebay, and I can see the product XML structure if I echo the whole thing to the page in the source code.

The problem is with section:

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


It fails silently without any errors. I do have error reporting turned on.

Any help much appreciated!

--- EDIT ---

The XML code response is structured as follows:

<!--?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>

Answer

Finally cracked it. After hours of frustration finally figured out that the xml echo path is case sensative. Only happened in the actual script becuase when I copied the xml (for the above example) from the html source code, it showed up all lower case. But the actual returned xml has mixed case in the xml nodes.

So,

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

needed to be:

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