通过 PHP 的 file_get_contents() 和 cURL ipinfo.io 失败



我尝试从 http://ipinfo.io 获取地理位置数据,

这是我的方式:

$resp = file_get_contents('http://ipinfo.io/json');
$data = json_decode($resp);

它返回一个错误:

Warning: file_get_contents(http://ipinfo.io/json): failed to open stream: Permission denied in ....

但是后来我在浏览器的 URL 框中手动访问链接 (http://ipinfo.io/json),它显示了正确的 json。

我也尝试使用 cURL :

$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, "ipinfo.io/json");
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curlSession);
if (FALSE === $resp) {
  echo curl_errno($curlSession);
} 
curl_close($curlSession);

它回显数字 7,我在互联网上查找,错误 7 表示无法连接到服务器。

知道为什么吗?

谢谢

我运行 http://ipinfo.io,我们不会阻止对任何 IP 的访问(我们确实会限制来自 IP 的请求,但这会导致 HTTP 状态代码,而不是阻塞的连接)。对我来说,这听起来像是您的服务器的配置问题。某些主机会锁定file_get_contents,使其无法打开 URL,或者可能已阻止 http://ipinfo.io。有几种方法可以追踪到这一点:

1)你可以打开另一个带有file_get_contents的网址吗?例如。当你file_get_contents('http://google.com')时会发生什么.如果您在那里收到权限被拒绝错误,那么您应该与您的托管服务提供商联系

2) 命令行curl适用于 ipinfo.io 吗?-i -v标志应该为您提供有关此处发生的情况的更多信息。以下是成功请求的外观:

$ curl -iv ipinfo.io
* Rebuilt URL to: ipinfo.io/
*   Trying 54.68.119.255...
* Connected to ipinfo.io (54.68.119.255) port 80 (#0)
> GET / HTTP/1.1
> Host: ipinfo.io
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Content-Type: application/json; charset=utf-8
Content-Type: application/json; charset=utf-8
< Date: Sun, 15 Jan 2017 18:38:44 GMT
Date: Sun, 15 Jan 2017 18:38:44 GMT
< Server: nginx/1.8.1
Server: nginx/1.8.1
< Set-Cookie: first_referrer=; Path=/
Set-Cookie: first_referrer=; Path=/
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< Content-Length: 252
Content-Length: 252
< Connection: keep-alive
Connection: keep-alive
<
{
  "ip": "24.6.61.239",
  "hostname": "c-24-6-61-239.hsd1.ca.comcast.net",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3845,-122.0881",
  "org": "AS7922 Comcast Cable Communications, LLC",
  "postal": "94040"
* Connection #0 to host ipinfo.io left intact
}

通常,服务器将配置为阻止请求标头中不存在User-Agent字符串的请求,因此您可以向file_get_contents添加一个context参数,以提供User-Agent和所需的任何其他标头。

$args=array(
    'http'=>array(
        'method' => "GET",
        'header' => implode( "n", array(
                    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0',
                    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Host: ipinfo.io'
                )
            )
        )
    );
/* create the context */
$context=stream_context_create( $args );
$resp = file_get_contents( 'http://ipinfo.io/json', FILE_TEXT, $context );
$data = json_decode( $resp );
echo '<pre>',print_r( $data,true ),'</pre>';

我在这里看到了 2 个合理的解释。

1:你使用了一个糟糕的DNS服务器。 试试GoogleDNS (8.8.8.8)。curl_setopt($curlSession,CURLOPT_DNS_LOCAL_IP4,'8.8.8.8');

如果可以修复它,请联系您的 DNS 提供商并与他们一起整理

2:你被IP禁止了。 尝试只为他们的IP创建一个TCP套接字,看看你是否可以做到这一点。

<?php
$sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
var_dump($sock,socket_connect($sock,gethostbyname('ipinfo.io'),80),socket_close($sock));

如果你做不到这一点,你可能被IP禁止了

最新更新