dslpofp041310584 2017-11-01 19:50
浏览 240

无法将Curl输出转换为JSON

Time for a second set of eyes. I've been looping on this for awhile. The url http://freegeoip.net/json/ appears to return JSON. I can't seem to decode or parse it. Any help would be greatly appreciated. Code follows:

    <?php

    //////////////////////////////////////////////////////////////////////
    //  Debug options
    //////////////////////////////////////////////////////////////////////
     error_reporting(E_ALL);
     error_reporting(E_ALL | E_STRICT);
     ini_set('display_errors', 1);

    function get_ip_address()
    {
        foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
            if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                return $ip;
            }
            }
        }
        }
    }

    $myIP = get_ip_address();

    $url = "http://freegeoip.net/json/" . $myIP;
    $cURL = curl_init();
    curl_setopt($cURL, CURLOPT_URL, $url);
    curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cURL, CURLOPT_HTTPGET, true);
    curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Accept: application/json'
    ));

    $result = curl_exec($cURL);
    $myArray = json_decode($result);

    curl_close($cURL);

    //////////////////////////////////////////////////////////////////////
    //  Debug  
    //////////////////////////////////////////////////////////////////////
     echo $myArray->region_code . "<br />";
     echo $myArray->region_name . "<br />";
     echo $myArray->city . "<br />";
     echo $myArray->zip_code . "<br />";
     echo $myArray->time_zone . "<br />";
     echo $myArray->latitude . "<br />";
     echo $myArray->longitude . "<br />";
     echo $myArray->metro_code . "<br />";
  • 写回答

1条回答 默认 最新

  • dtotuki47568 2017-11-01 20:02
    关注

    Edit : Removed answer.

    I put the code into my server and it works perfectly fine for me.

    Maybe curl is disabled on his server?

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况