2016-03-15 55 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。谢谢一堆。 –