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 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器