2015-07-19 84 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

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