Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在pc之外获取地理位置信息”;LocalHost";?_Php_Ip Geolocation - Fatal编程技术网

Php 如何在pc之外获取地理位置信息”;LocalHost";?

Php 如何在pc之外获取地理位置信息”;LocalHost";?,php,ip-geolocation,Php,Ip Geolocation,我想使用外部服务(网站服务)获取一些地理位置信息,但这只能在我的pc上正常工作(使用WampServerhttp客户端的本地主机);当我的php文件被传输到远程主机(pc)时,它无法获取这些信息。已经尝试了几项主机服务,但仍然没有成功 注:我的网站参考是 这是我的密码: $ip = $_REQUEST['REMOTE_ADDR']; $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip)); if($quer

我想使用外部服务(网站服务)获取一些地理位置信息,但这只能在我的pc上正常工作(使用
WampServer
http客户端的本地主机);当我的php文件被传输到远程主机(pc)时,它无法获取这些信息。已经尝试了几项主机服务,但仍然没有成功

注:我的网站参考是

这是我的密码:

$ip = $_REQUEST['REMOTE_ADDR']; 
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));

if($query && $query['status'] == 'success') {

  $regiao = $query['regionName'];
  $cidade = $query['city'];
  $isp = $query['isp'];
  $sigla_regiao = $query['region'];
} 
else {
  echo '';
}
所以,有人有一些想法为什么这个api不能在远程主机上工作


提前感谢。

根据您发布的错误,文件获取内容功能无法创建到远程主机的连接。某些功能可能会禁用对远程资源的访问

您可以尝试使用cURL访问远程URL:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://ip-api.com/php" . $ipAddr);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec ($curl);
curl_close ($curl); 

if (curl_errno($curl))
    throw new \Exception(sprintf("Connection error %s: %s", curl_errno($curl), curl_error($curl)));

print_r(unserialize($content));

如果您对其他提供商感兴趣,您可以使用

一个有效的JSON示例:

<?php

    $json = file_get_contents('https://geoip-db.com/json');
    $data = json_decode($json);

    print $data->country_code . '<br>';
    print $data->country_name . '<br>';
    print $data->state . '<br>';
    print $data->city . '<br>';
    print $data->postal . '<br>';
    print $data->latitude . '<br>';
    print $data->longitude . '<br>';
    print $data->IPv4 . '<br>';

?>

或使用php的JSONP回调:

<?php

    $jsonp = file_get_contents('https://geoip-db.com/jsonp');
    $data = jsonp_decode($jsonp);

    print $data->country_code . '<br>';
    print $data->country_name . '<br>';
    print $data->state . '<br>';
    print $data->city . '<br>';
    print $data->postal . '<br>';
    print $data->latitude . '<br>';
    print $data->longitude . '<br>';
    print $data->IPv4 . '<br>';

    // Strip callback function name and parenthesis
    function jsonp_decode($jsonp) { 
        if($jsonp[0] !== '[' && $jsonp[0] !== '{') { 
           $jsonp = substr($jsonp, strpos($jsonp, '('));
        }
        return json_decode(trim($jsonp,'();'));
    }
?>


您的主机可能会阻止您对该API的请求。如果不在第二行中抑制错误输出,您是否会收到某种错误?@lxg,放入
var\u dump($query)
,返回:bool(false)No,我的意思是:请删除
@
登录第2行,并告诉我们您是否收到错误消息。@lxg,是出现的错误。