php 无法连接到 www.googleapis.com 端口 443:网络无法访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28366402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:49:31  来源:igfitidea点击:

Failed to connect to www.googleapis.com port 443: Network unreachable

phpsymfonygoogle-logingoogle-client

提问by Reza Abbasi

I'm trying to implement google sign-in in my website that is written in Symfony2 using Google Client API. I followed the instructions here, but when I call $client->authenticate($code); command an exception is thrown telling that:
Failed to connect to www.googleapis.com port 443: Network unreachable
What is the problem?

我正在尝试在使用Google Client API用 Symfony2 编写的网站中实现 google 登录。我按照这里的说明操作,但是当我调用 $client->authenticate($code); 命令抛出异常,说明:
无法连接到 www.googleapis.com 端口 443:网络无法访问
是什么问题?

回答by le0diaz

Solution 2 (from update). it seems to be some bug with ipv6 interfaces and curl with php, so another solution is to set the appropiate option in curl function to connect to the remote server with ipv4 in your php script

解决方案 2(来自更新)。这似乎是 ipv6 接口和 curl 与 php 的一些错误,所以另一种解决方案是在 curl 函数中设置适当的选项,以在您的 php 脚本中使用 ipv4 连接到远程服务器

curl_setopt( $curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

curl_setopt( $curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);



Solution 1.

解决方案1。

The same happened to me (solved by adding the ip v4 and hostname to /etc/hosts file), As pointed by @hans-z in another answer comment, I tried using next command to validate the network issue.

我也遇到了同样的情况(通过将 ip v4 和主机名添加到 /etc/hosts 文件来解决),正如@hans-z 在另一个答案评论中指出的那样,我尝试使用 next 命令来验证网络问题。

curl -v https://www.googleapis.com 

It turned out that it was randomly, and only 1 in 10 tries the curl command could connect to the server, the response was something like: ("La red es inaccesible" means in english "network unreachable" :) )

结果是随机的,只有十分之一的尝试 curl 命令可以连接到服务器,响应类似于:(“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...
* 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

And the successfull response was:

成功的回应是:

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

The weird message was "Hostname was NOT found in DNS cache", I was using google DNS's 4.4.4.4 and 8.8.8.8, so I added that domain to my /etc/hosts file using the ipv4 from the curl command output and the problem was solved.

奇怪的消息是“在 DNS 缓存中未找到主机名”,我使用的是 google DNS 的 4.4.4.4 和 8.8.8.8,所以我使用 curl 命令输出中的 ipv4 将该域添加到我的 /etc/hosts 文件中,并解决了问题解决了。

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

回答by prince jose

If your network under proxy. You should set the proxy url and port

如果您的网络在代理下。您应该设置代理网址和端口

curl_setopt($ch, CURLOPT_PROXY, "http://url.com"); //your proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, "80"); // your proxy port number

This is solves my problem

这是解决我的问题

回答by Hans Z.

Looks like an outgoing firewall/proxy/network issue. You should be able to verify that using pingand curlfrom the machine where your website runs.

看起来像是传出防火墙/代理/网络问题。您应该能够验证使用ping,并curl从机器在您的网站上运行。

回答by Vikas Chauhan

Try this echo ipv4 >> ~/.curlrc. It Worked for me.

试试这个 echo ipv4 >> ~/.curlrc。它对我有用。

回答by cajma

Had the same issue - problem was with dns resolution, like mentioned in le0diaz's answer.

有同样的问题 - 问题出在 dns 分辨率上,就像 le0diaz 的回答中提到的那样。

On ubuntu 16.04, we were able to solve it by chaning /etc/gai.conf, changing the presedance for IPv6, uncommenting the following line: precedence ::ffff:0:0/96 100

在 ubuntu 16.04 上,我们可以通过更改 /etc/gai.conf、更改 IPv6 的优先级、取消注释以下行来解决它:precedence ::ffff:0:0/96 100