Search code examples
phpjsonebay-api

Why Would Json_decode() return NULL? (parsing ebay api in php)


A curious problem here and I'm sure the answer is staring me right in the face but I cannot see it.
I have called some data from Ebays api and I have recieved a json file in response.
To help me debug an potential errors, I use var_dump() to see what the data looks like before and after it has been decoded but in this case when i use var_dump() after i have used json_decode() it comes up NULL.

I have used this code for two other sites and I cannot see why this is has crept in. Here is the code I am using:

$url = "http://svcs.ebay.co.uk/services/search/FindingService/v1?SECURITY-APPNAME=(App Id)&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&GLOBAL-ID=EBAY-GB&REST-PAYLOAD&keywords=Iphone&paginationInput.entriesPerPage=2";
$headers = array( "Content-type: application/json;charset=\"utf-8\"",  "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"run\"");
$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($cURL);

$json=json_decode($result, true);

echo"<br>result in json <br>";
var_dump($result);

echo "<br><br> Json Decoded <br>";
var_dump($json);
curl_close($cURL);

the result is:

result in json
string(3166) "/**/_cb_findItemsByKeywords({"findItemsByKeywordsResponse":[{"ack":["Success"],"version":["1.13.0"],"timestamp":["2015-07-19T22:17:01.579Z"],"searchResult":[{"@count":"2","item":[{"itemId":["131555668833"],"title":["Apple iPhone 4 - 16GB - Black (Orange) Smartphone"],"globalId":["EBAY-GB"],"primaryCategory":[{"categoryId":["9355"],"categoryName":["Mobile & Smart Phones"]}],"galleryURL":["http:\/\/thumbs2.ebaystatic.com\/m\/m4DvclaIDHqh3yoWm-57LQg\/140.jpg"],"viewItemURL":["http:\/\/www.ebay.co.uk\/itm\/Apple-iPhone-4-16GB-Black-Orange-Smartphone-\/131555668833?pt=LH_DefaultDomain_3"],"productId":[{"@type":"ReferenceID","__value__":"102596407"}],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["BR27JR"],"location":["Bromley,United Kingdom"],"country":["GB"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"GBP","__value__":"8.0"}],"shippingType":["FlatDomesticCalculatedInternational"],"shipToLocations":["US","CA","GB","AU","AT","BE","FR","DE","IT","JP","ES","NL","BG","HR","CY","CZ","DK","FI","GR","HU","IE","LT","LU","MT","PL","PT","RO","SK","SI","SE","RU","NZ","IL","NO"]}],"sellingStatus":[{"currentPrice":[{"@currencyId":"GBP","__value__":"50.0"}],"convertedCurrentPrice":[{"@currencyId":"GBP","__value__":"50.0"}],"bidCount":["0"],"sellingState":["Active"],"timeLeft":["P0DT0H3M17S"]}],"listingInfo":[{"bestOfferEnabled":["false"],"buyItNowAvailable":["false"],"startTime":["2015-07-12T22:20:18.000Z"],"endTime":["2015-07-19T22:20:18.000Z"],"listingType":["Auction"],"gift":["false"]}],"condition":[{"conditionId":["3000"],"conditionDisplayName":["Used"]}],"isMultiVariationListing":["false"],"topRatedListing":["false"]},{"itemId":["311405341211"],"title":["Apple Iphone 6 16GB Gold EE Network UK. Faulty Faulty."],"globalId":["EBAY-GB"],"primaryCategory":[{"categoryId":["9355"],"categoryName":["Mobile & Smart Phones"]}],"galleryURL":["http:\/\/thumbs4.ebaystatic.com\/m\/m0vIGIioZxGpyu1J_KTJ2bw\/140.jpg"],"viewItemURL":["http:\/\/www.ebay.co.uk\/itm\/Apple-Iphone-6-16GB-Gold-EE-Network-UK-Faulty-Faulty-\/311405341211?pt=LH_DefaultDomain_3"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["UB25UW"],"location":["Southall,United Kingdom"],"country":["GB"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"GBP","__value__":"8.75"}],"shippingType":["FlatDomesticCalculatedInternational"],"shipToLocations":["GB","NO"]}],"sellingStatus":[{"currentPrice":[{"@currencyId":"GBP","__value__":"270.0"}],"convertedCurrentPrice":[{"@currencyId":"GBP","__value__":"270.0"}],"bidCount":["24"],"sellingState":["Active"],"timeLeft":["P0DT0H6M4S"]}],"listingInfo":[{"bestOfferEnabled":["false"],"buyItNowAvailable":["false"],"startTime":["2015-07-18T22:23:05.000Z"],"endTime":["2015-07-19T22:23:05.000Z"],"listingType":["Auction"],"gift":["false"]}],"condition":[{"conditionId":["7000"],"conditionDisplayName":["For parts or not working"]}],"isMultiVariationListing":["false"],"topRatedListing":["false"]}]}],"paginationOutput":[{"pageNumber":["1"],"entriesPerPage":["2"],"totalPages":["1183420"],"totalEntries":["2366839"]}],"itemSearchURL":["http:\/\/www.ebay.co.uk\/sch\/i.html?_nkw=Iphone&_ddo=1&_ipg=2&_pgn=1"]}]})"

Json Decoded
NULL 

Any ideas where i have gone wrong with this?


Solution

  • Your JSON contains /**/_cb_findItemsByKeywords(, which malformes the JSON string. json_decode returns NULL if the string cannot be parsed.