douzhanshen0657 2016-06-27 11:29
浏览 150


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 = '';
//set options
            curl_setopt($ch, CURLOPT_URL, $priceURL);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $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...


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



    • ¥15 复杂表达式求值程序里的函数优先级问题
    • ¥15 求密码学的客成社记ji着用
    • ¥35 POI导入树状结构excle
    • ¥15 初学者c语言题目解答
    • ¥15 div editable中的光标问题
    • ¥15 mysql报错1415Not allowed to return a result set from a trigger 不知如何修改
    • ¥60 Python输出Excel数据整理,算法较为复杂
    • ¥15 回答几个问题 关于数据库
    • ¥15 51单片机串口通信问题,未完成且要修改
    • ¥15 百鸡问题 c++编程问题(相关搜索:输出数据)