【问题标题】:I am trying to get user country of location from geoplugin.net but i am getting an error我正在尝试从 geoplugin.net 获取用户所在国家/地区,但出现错误
【发布时间】:2018-11-27 02:46:58
【问题描述】:

我得到的错误是:

警告: file_get_contents(http://maps.google.com/maps/api/geocode/json?address=&sensor=false) [function.file-get-contents]:无法打开流:HTTP 请求 失败的! HTTP/1.0 400 错误请求 D:\xampp\htdocs\SwaziTour\index.php 第 57 行

我的代码如下:

echo var_export( unserialize( file_get_contents( 'http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'] ) ) );

【问题讨论】:

标签: php


【解决方案1】:

不要使用 geoplugin.net。试试这个:

$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
print_r($details);

【讨论】:

  • 我在您的代码中收到此错误:stdClass Object ( [ip] => ::1 [bogon] => 1 ) 警告:file_get_contents(maps.google.com/maps/api/geocode/json?address=&sensor=false) [function.file-get-内容]:打开流失败:HTTP 请求失败!第 59 行 D:\xampp\htdocs\SwaziTour\index.php 中的 HTTP/1.0 400 错误请求
  • 代码获取不到IP地址,问题就出在这里
  • 你在本地尝试吗?
  • 啊这就是原因,在 localhost 访问者的 ip 地址中是这样的 ::1
  • 它返回那个
【解决方案2】:

您是否尝试在浏览器中打开您的链接?如果你打开你的 url,你会看到一个错误,因为你没有地址。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 2022-11-17
  • 2022-11-13
  • 2018-01-30
相关资源
最近更新 更多