2015-07-19 82 views
0

这里有一个好奇的问题,我相信答案是在脸上凝视着我,但我看不到它。
我已经从Ebays API调用了一些数据,并且收到了一个json文件作为回应。
要帮我调试一个潜在的错误,我用var_dump(),看看有什么数据看起来像之前和之后已解码,但在这种情况下,当我用var_dump()我已经使用json_decode(后),它出现NULL 。为什么Json_decode()返回NULL? (在php中解析ebay api)

我已经使用这个代码,其他两个网站,我不明白为什么这是已经在悄悄地这里是我使用的代码:

$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); 

结果是:

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 

任何想法,我出了这个问题?

+1

'当输入字符串不是有效的JSON –

+0

这是在URL中我要求采用JSON格式奇怪json_decode'返回null。我甚至已经将它编码为utf 8,但在ebays文档中默认情况下它会返回utf8中的json。 我注释掉了: curl_setopt($ cURL,CURLOPT_HTTPGET,true);和 curl_setopt($ cURL,CURLOPT_HTTPHEADER,$ headers);但是我得到了相同的结果 –

+3

您的JSON格式错误字符串'/ **/_ cb_findItemsByKeywords('是原因 尝试删除'$ url'中的'&callback = _cb_findItemsByKeywords' –

回答

2

你的JSON包含/**/_cb_findItemsByKeywords(,其中malformes JSON字符串。如果字符串不能被解析,则返回NULL。

+0

这应该是一个评论(已经存在的OP) –

+0

为什么?这是OP的问题的答案。 – Hedam

+0

我感到困惑另一篇文章,抱歉:) –