【发布时间】:2014-09-05 11:03:04
【问题描述】:
我正在尝试使用 PHP 为我的一个项目获取客户端的时区。在 stackoverflow 中看到一篇文章,其中包含我正在寻找的解决方案。这是该帖子的链接。
我遵循了最受好评的解决方案,但它产生了类似这样的错误。
假设$_SERVER['REMOTE_ADDR'] 将212.88.207.202 返回到$ip,我在开发阶段将该值硬编码为$ip。
没关系,因为硬编码 212.88.207.202 或使用 $_SERVER['REMOTE_ADDR'] 这会将相同的值返回给 $ip。
但是当我运行页面时,我得到了这个错误。
Warning: file_get_contents(http://smart-ip.net/geoip-json/212.88.207.202): failed to open stream: No connection could be made because the target machine actively refused it.
这是代码
<?php
$ip = '212.88.207.202'; // means we got user's IP address
$json = file_get_contents( 'http://smart-ip.net/geoip-json/' . $ip); // this one service we gonna use to obtain timezone by IP
// maybe it's good to add some checks (if/else you've got an answer and if json could be decoded, etc.)
$ipData = json_decode($json, true);
//var_dump($ipData);
if ($ipData['timezone']) {
$tz = new DateTimeZone( $ipData['timezone']);
$now = new DateTime( 'now', $tz); // DateTime object corellated to user's timezone
} else {
// we can't determine a timezone - do something else...
}
?>
我哪里做错了???
【问题讨论】:
-
由于某些情况,目标计算机已断开您的连接。这可能有很多原因,具体取决于服务条件。您的 IP 可能由于某种原因被列入黑名单,或者该服务目前不可用。您传递的参数也可能不正确。如果您不发布更多代码,我们将无法在这里为您提供帮助。
-
谢谢,看来问题已经出在 smart-ip.net 我无法在浏览器中打开它。这是什么?这是一项开放服务还是您自己的域?
-
主动拒绝你的连接通常意味着目标机器上没有运行网络服务器。该服务似乎不再运行。
-
这是一项开放服务。 @斯坦尼
-
@Steini 美国和加拿大有 6 个时区,因此您需要做的不仅仅是确定国家/地区。您还需要了解每个国家/地区何时更改为 DST 的规则。
标签: php datetime timezone php-5.3