0

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?

5
  • 1
    json_decode returns null when the input string is not a valid json Commented Jul 19, 2015 at 22:23
  • That is strange in the url I ask for the format to be in json. I even have it encoded to utf 8 for good measure though in ebays documentation it returns json in utf8 by default. I commented out: curl_setopt($cURL, CURLOPT_HTTPGET, true); and curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers); but I got the same result Commented Jul 19, 2015 at 22:28
  • 3
    Your JSON is malformed the string /**/_cb_findItemsByKeywords( is the cause. Try to remove the &callback=_cb_findItemsByKeywords in $url Commented Jul 19, 2015 at 22:29
  • Thanks E-learner I +1 your comment so i hope you get some rep for that :) Commented Jul 19, 2015 at 23:07
  • @benjayhutton no rep is added for up voting comments Commented Jul 20, 2015 at 0:16

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Why? It's an answer to OP's question.
I was confused about another post, sorry :)
Thanks for pointing it out, the problem was the "callback=_cb_findItemsByKeywords" part of the url. For some reason it alwasy put /**/ at the front and ruined the whole thing and as it was one long string I couldn't get the function Trim() to get rid of it (maybe i wasn't using it properly but that is a different issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.