连接www.googleapis.com的443端口失败:网络不可达。

5
我正在尝试在使用Symfony2编写的网站中实现Google登录,使用Google Client API。我按照这里的说明进行操作,但是当我调用$client->authenticate($code);命令时,会抛出一个异常,告诉我:
无法连接到www.googleapis.com端口443:网络不可达
问题出在哪里?

请检查您的防火墙配置,请参考此链接:http://stackoverflow.com/a/39590519/2652524 - Gujarat Santana
5个回答

7
方案2(来自更新)。 似乎存在ipv6接口和curl与php的某些bug,因此另一种解决方案是在您的php脚本中将curl函数的适当选项设置为使用ipv4连接到远程服务器。
curl_setopt($curl,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
方案1。 我遇到了同样的问题(通过将ip v4和主机名添加到/etc/hosts文件中解决),正如@hans-z在另一个答案评论中指出的那样,我尝试使用下一个命令验证网络问题。
curl -v https://www.googleapis.com 

事实证明,curl命令连接服务器是随机的,只有10次尝试中的1次能够成功连接,响应信息可能如下所示: ("La red es inaccesible" 的英文意思是 "network unreachable" :) )

$ curl -v https://www.googleapis.com
* Rebuilt URL to: https://www.googleapis.com/
* Hostname was NOT found in DNS cache
*   Trying 64.233.186.95...
*   Trying 2800:3f0:4003:c00::5f...
* connect to 2800:3f0:4003:c00::5f port 443 failed: La red es inaccesible
* Failed to connect to www.googleapis.com port 443: La red es inaccesible
* Closing connection 0
curl: (7) Failed to connect to www.googleapis.com port 443: La red es inaccesible

成功的响应如下:

curl -v https://www.googleapis.com
* Rebuilt URL to: https://www.googleapis.com/
* Hostname was NOT found in DNS cache
*   Trying 64.233.186.95...
*   Trying 2800:3f0:4003:c00::5f...
* Connected to www.googleapis.com (64.233.186.95) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
*    subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.storage.googleapis.com
*    start date: 2015-04-08 14:12:01 GMT
*    expire date: 2015-07-07 00:00:00 GMT
*    subjectAltName: www.googleapis.com matched
*    issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*    SSL certificate verify ok.
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.googleapis.com
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Date: Tue, 21 Apr 2015 21:38:12 GMT
< Vary: X-Origin
< Content-Type: text/html; charset=UTF-8
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
* Server GSE is not blacklisted
< Server: GSE
< Alternate-Protocol: 443:quic,p=1
< Accept-Ranges: none
< Vary: Origin,Accept-Encoding
< Transfer-Encoding: chunked
< 
* Connection #0 to host www.googleapis.com left intact

奇怪的消息是“Hostname was NOT found in DNS cache”,我正在使用谷歌DNS的4.4.4.4和8.8.8.8,所以我将该域添加到我的/etc/hosts文件中,使用curl命令输出的ipv4地址并解决了这个问题。

#/etc/hosts
...
64.233.186.95 www.googleapis.com
#DONT COPY THIS IP, USE THE ONE FROM YOUR CURL OUTPUT
...

这立即解决了我在本地开发中使用MAMP、Apache/2.2.29、PHP 5.6.10和curl 7.41.0尝试连接Google GCM API时遇到的超时问题。奇怪的是,并非所有请求都会超时,但很多确实如此。感谢您的帮助! - m_katsifarakis

3

看起来是出站防火墙/代理/网络问题。您可以使用pingcurl从托管您的网站的机器上验证这一点。


我正在本地测试它。这是问题吗? - Reza Abbasi
1
我在我的VPS上尝试了一下,它打印出了404未找到响应。 - Reza Abbasi
当我在浏览器中输入网址时,大多数情况下会显示“未找到”,很少会重定向到https://www.google.com/?gws_rd=ssl。 - Reza Abbasi
有时候我也会遇到这种情况,从我的本地网络中(随机)解析主机名时,使用curl命令有时会得到以下信息:
  • 主机名未在DNS缓存中找到
  • 尝试连接64.233.186.95...
  • 尝试连接2800:3f0:4003:c00::5f...
  • 已连接到www.googleapis.com (64.233.186.95) 443端口 (#0)
  • 成功设置证书验证位置:
  • CA文件:无 CA路径:/etc/ssl/certs ...
有时候会出现错误:连接到2800:3f0:4003:c00::5f 443端口失败:网络不可访问
- le0diaz
请从您的服务器尝试使用 telnet android.googleapis.com 443 命令来检查是否存在防火墙问题。 - Hirdesh Vishwdewa
显示剩余2条评论

3
如果您的网络在代理下,请设置代理URL和端口。
curl_setopt($ch, CURLOPT_PROXY, "http://url.com"); //your proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, "80"); // your proxy port number

这解决了我的问题。

1
尝试执行以下命令:echo ipv4 >> ~/.curlrc。这对我起作用了。

这应该是一条注释,而不是答案! - Divyang Desai
2
抱歉Div,我无法进行评论,所以我回答了。 - Vikas Chauhan

0

遇到了同样的问题 - 问题出在 DNS 解析上,就像 le0diaz 的回答中提到的那样。

在 Ubuntu 16.04 上,我们通过更改 /etc/gai.conf 文件解决了这个问题,将 IPv6 的优先级更改为以下行的取消注释: precedence ::ffff:0:0/96 100


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接