TK421 TK421 - 1 year ago 99
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:

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

$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="">';
$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',

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

// return the response XML
return $responseXML;




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="">
<searchresult count="1">
<title>Item Title</title>
<categoryname>Category Name</categoryname>
<galleryurl>Galery URL</galleryurl>
<viewitemurl>Item URL</viewitemurl>
<shippingservicecost currencyid="USD">0.0</shippingservicecost>
<currentprice currencyid="USD">15.99</currentprice>
<convertedcurrentprice currencyid="USD">15.99</convertedcurrentprice>

Answer Source

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.


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

needed to be:

    echo $xmlResponse -> searchResult -> item[0] -> title;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download