question

improveyourlife avatar image
0 Likes"
improveyourlife asked

Using GetSingleItem with SimpleXMLElement

I'm using finding service has URL and it works fine, $url = ' http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsAdvanced'; $url .= '&SERVICE-NAME=FindingService&SERVICE-VERSION=1.0.0&GLOBAL-ID=EBAY-GB'; $url .= '&SECURITY-APPNAME=' . $appid . '&RESPONSE-DATA-FORMAT=XML'; $url .='&keywords=' . $itemnumber; I now need to call GetSingleItem $url2 = ' http://open.api.ebay.com/shopping?callname=GetSingleItem&responseencoding=XML&appid=' . $appid . '&siteid=0&version=967&ItemID=' . $itemnumber . "&IncludeSelector=Description,ItemSpecifics"; Both return success using $resp2 = file_get_contents($url2); but when i use $xml2 = new SimpleXMLElement($resp2); it returns a non object, how can i call the elements from resp2 for its details similar too $itemid = (string) $xml->searchResult->item->itemId; $shipcost = (string) $xml->searchResult->item->shippingInfo->shippingServiceCost;
apitrading api
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

clickimusprime avatar image
0 Likes"
clickimusprime answered
SimpleXMLElement should work. Check $resp2 to see if it's proper XML.
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

helios825 avatar image
0 Likes"
helios825 answered
Instead of using *new SimpleXMLElement()* , use [simplexml_load_string()][1] to convert the retrieved API data (a long string) to parsed XML. You should then be able to use code like *$itemid = (string) $xml->searchResult->item->itemId;* just fine. [1]: http://php.net/manual/en/function.simplexml-load-string.php
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

clickimusprime avatar image
0 Likes"
clickimusprime answered
The two methods are the same except for some differences in calling parameters and how exceptions are indicated. They return identical objects.
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

improveyourlife avatar image
0 Likes"
improveyourlife answered
How do i check $resp2 to see if its proper XML? both are executed the same so should have a result no?
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

clickimusprime avatar image
0 Likes"
clickimusprime answered
echo it and read, or file_put_contents to a file and open the file in a browser. In most cases, eBay will return a well formatted XML response with error messages if your request is incorrect. So you probably have a error in your PHP process somewhere. You have to show your complete code if you want help with that. Posting scraps of lines won't tell us much. If resp2 is ok, look at this page: http://php.net/manual/en/simplexml.examples-errors.php
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

improveyourlife avatar image
0 Likes"
improveyourlife answered
Here is my url $url2 = ' http://open.api.ebay.com/shopping?callname=GetSingleItem&responseencoding=XML&appid=' . $appid . '&siteid=0&version=967&ItemID=' . $itemnumber . "&IncludeSelector=Description,ItemSpecifics"; here is the next instructions $resp2 = file_get_contents($url2); error for the above is Warning: file_get_contents( http://open.api.ebay.com/shopping?callname=GetSingleItem&responseencoding=XML&appid=myapiid-55ec0110&siteid=0&version=967&ItemID=192401461174 &IncludeSelector=Description,ItemSpecifics): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in $xml2 = new SimpleXMLElement($resp2); here is my error for this response2 is - Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in import_ebay.php:97 Stack trace: #0 import_ebay.php(97): SimpleXMLElement->__construct('') #1 {main} thrown Hope this helps Gary
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

clickimusprime avatar image
0 Likes"
clickimusprime answered
That's different than what you described before. You seem to be ignoring the 400 error occurring in file_get_contents. That means resp2 is bad. Can't tell why that's happening without the relevant code. But the URL is probably wrong (probably a space in there somewhere). Add one or both of the following before the file_get_contents line: echo "$url2
\n"; error_log($url2); That will show you the request URL actually being submitted to eBay. Fix the URL.
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

improveyourlife avatar image
0 Likes"
improveyourlife answered
I did find a space, thank you, if i wanted to pull out just the quantity would i just put that at the end of the url2 or call it from resp2?
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

improveyourlife avatar image
0 Likes"
improveyourlife answered
I have got rid of the errors, only thing now is how do i extract the quantity of that item? is it from xml2? when i use this method same as $url findItemsAdvanced i get a non object error $qty = (string) $xml2->searchResult->item->Quantity;
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

clickimusprime avatar image
0 Likes"
clickimusprime answered
Since the Shopping API returns different data, it has a different format and doesn't return a searchResult node. Look at the docs or the XML response itself to determine where the data you need is located. Also, FWIW, if you already have the item number the call to findItemsAdvanced may not be necessary. GetSingleItem/GetMultiplItem return a lot of the same data. Depends on your needs.
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.