douzhanshen0657 2016-06-27 11:29
浏览 151
已采纳

从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

  • 写回答

1条回答 默认 最新

  • du8589840 2016-06-27 11:49
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog