【发布时间】:2013-03-27 22:45:05
【问题描述】:
我正在使用以下 API 通过 IP 获取国家/地区代码
http://api.hostip.info/country.php?ip=' . $IP
示例:在 Localhost
上$IP = '202.71.158.30';
//pass the ip as a parameter for follow URL it will return the country
$country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);
它在这里工作正常并显示国家代码。
但它在服务器上显示错误
例子:
$IP=$_SERVER['REMOTE_ADDR'];
$country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);
显示以下错误:
警告: file_get_contents(http://api.hostip.info/country.php?ip=101.63.xx.xxx) [function.file-get-contents]:无法打开流:连接 在 /srv/disk4/1322145/www/servername.in/app/header.php 中被拒绝 在第 12 行
这有什么问题?
【问题讨论】:
-
这可能与 PHP 关系不大,更多的是与网络限制有关。尝试在
file_get_contents之后转储$http_response_header以获取有关失败原因的更多信息。 -
确保您可以使用 fopen 打开一个 url。见:php.net/…
-
@MarcellFülöp :但它在 localhost 上工作仅在服务器上显示错误。那么是 $_SERVER['REMOTE_ADDR']; 的问题吗? ?
-
@MarcellFülöp : var_dump($country_code);显示布尔(假)
-
我的意思是
$http_response_header。当file_get_contents与 HTTP 包装器一起使用时,PHP 会填充此变量,并且可能会提供有关 HTTP 请求的有用信息。
标签: php file-get-contents