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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效