dragon4587 2019-03-04 18:00
浏览 74
已采纳

JSON使用PHP从db-ip.com解码api

I have some problem with db-ip.com api that display info about visitor ip's. My script only print < pre > tags but nothing between. I need that all parameters from api are decoded and print, like from this link: http://db-ip.com/178.133.109.106. Please help me.

<?php

// get ip
if (!empty($_SERVER["HTTP_CLIENT_IP"])){

    $ip = $_SERVER["HTTP_CLIENT_IP"];

}

elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){

    $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];

}

else {

    $ip = $_SERVER["REMOTE_ADDR"];

}



$json_url = "http://api.db-ip.com/v2/free/$ip";
$json = file_get_contents($json_url);
$json=str_replace('}, ]',"} ]",$json);
$data = json_decode($json);

echo "<pre>";
print_r($data);
echo "</pre>";
?>
  • 写回答

1条回答 默认 最新

  • douyuefei3546 2019-03-04 22:48
    关注

    Try this code

    <?php
        header('Content-Type: application/json');
        // get ip
        $ip = $_SERVER['HTTP_CLIENT_IP'] ? $_SERVER['HTTP_CLIENT_IP'] : ($_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']); //$ip = '178.133.109.106';
    
        $json_url = "http://api.db-ip.com/v2/free/$ip";
        $data = file_get_contents($json_url);
        print_r($data);
    
    ?>
    

    check out this link
    http://tpcg.io/Gv3KLv

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

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题