我试着从http://ipinfo.io获取GeoLocation数据,
这是我的方法:
$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 ....但是当我在浏览器的地址框中手动访问链接(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意味着无法连接到服务器。
知道为什么吗?
谢谢
发布于 2017-01-16 02:40:59
我运行http://ipinfo.io,我们不会阻止对任何in的访问(我们对来自in的请求进行速率限制,但这会导致HTTP状态代码,而不是阻止的连接)。对我来说,这听起来像是你的服务器的配置问题。一些主机锁定了file_get_contents,因此它无法打开URL,或者可能已经阻止了http://ipinfo.io。有几种方法可以追踪到这一点:
1)你能用file_get_contents打开另一个URL吗?例如:当你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
}发布于 2017-01-14 22:07:29
通常,服务器会被配置为阻止请求头部中没有User-Agent字符串的请求,因此您可以向file_get_contents添加一个context参数,以提供用户代理和所需的任何其他头部。
$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>';发布于 2017-01-14 22:23:35
我在这里看到了两个合理的解释。
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
https://stackoverflow.com/questions/41650787
复制相似问题