从curl获取特定数据
问题描述:
I want to get the value of the variable in the JSON but it keeps on giving me undefined index where the index is definitely there when I open the URL.
Here is my code:
//initializing cURL
$ch = curl_init();
//setting the URL
$priceURL = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=570&market_hash_name=Exalted%20Manifold%20Paradox';
//set options
curl_setopt($ch, CURLOPT_URL, $priceURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$string1 = curl_exec($ch);
//close curl session/free resources
curl_close ($ch);
//decode the json string into an array
$json_price_file = json_decode($string1, true);
$price = $json_price_file ["lowest_price"];
It gives an error of Undefined index: lowest_price. However, when you open the URL, the variable is definitely there. What am I missing? Please help :D
答
i confirm that your code works well, like Wazelin said, check your php_curl in the php.ini.
Edit: after looking at your code (by email) you have to add this :
After you get the $market_hash_name
value you need to replace the white space by %20 (valid url).
$market_hash_name = str_replace(' ', '%20', $market_hash_name);
Then i advise you to add an condition for the lowest_price to avoid errors :
if (isset($json_price_file["lowest_price"])) {
#your code...
}
Regards.