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

问题描述 投票:0回答:1

我对db-ip.com api有一些问题,它显示有关访问者ip的信息。我的脚本只打印<pre>标签,但没有。我需要来自api的所有参数都被解码和打印,就像这个链接:http://db-ip.com/178.133.109.106。请帮我。

<?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>";
?>
php json api curl file-get-contents
1个回答
1
投票

试试这个代码

<?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);

?>

看看这个链接 http://tpcg.io/Gv3KLv

© www.soinside.com 2019 - 2024. All rights reserved.