2016-03-15 52 views
-1

我正試圖解析從下面的URL JSON。但是,當我運行var_dump(json_decode($result, true))時,它返回NULL。但是當我複製echo $item_url的URL時,它會返回正確的JSON。試圖使用cURL來解析JSON-var_dump返回NULL

我已閱讀的另一個問題是var_dump(json_decode($result, true));將返回與空格的字符串,並且可能是一個問題

$item_url = "http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=" . $rgDescriptions->market_hash_name; 

echo $item_url; 

$curl_handle = curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, urlencode($item_url)); 
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name'); 
$result = curl_exec($curl_handle); 
curl_close($curl_handle); 

var_dump(json_decode($result, true)); 

貝婁是,可能會通過一個URL的一個例子。

http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=AK-47%20|%20Aquamarine%20Revenge%20(Minimal%20Wear)

+0

檢查這個回答類似的問題:http://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data – spaniard

+0

這裏第二個參數可能是問題;試試這個var_dump(json_decode($ result)); – itzmukeshy7

回答

1

它在你的代碼更改行後爲我工作。我認爲你需要編碼$rgDescriptions->market_hash_name只有不完整的網址。

變化

curl_setopt($curl_handle, CURLOPT_URL, urlencode($item_url)); 

TO

curl_setopt($curl_handle, CURLOPT_URL, $item_url); 

全碼

$item_url = "http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=AK-47%20|%20Aquamarine%20Revenge%20(Minimal%20Wear)"; 

$curl_handle = curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, $item_url); 
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name'); 
$result = curl_exec($curl_handle); 
curl_close($curl_handle); 

var_dump(json_decode($result, true)); 

輸出:

array(4) { 
    ["success"]=> 
    bool(true) 
    ["lowest_price"]=> 
    string(6) "$26.38" 
    ["volume"]=> 
    string(3) "133" 
    ["median_price"]=> 
    string(6) "$26.35" 
} 
+1

你說得對,我需要編碼'$ rgDescriptions-> market_hash_name'而不是整個URL。謝謝一堆。 –