php stream_socket_client 无法连接(连接超时)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1769189/
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 03:46:50  来源:igfitidea点击:

stream_socket_client unable to connect (connection timed out)

phpsocketsstreamssl-certificate

提问by Biranchi

I am using the certificate, and the private key

我正在使用证书和私钥

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $certfile);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.xyz.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

Its running in my local XAMPP Server, but its not working in the external server:

它在我的本地 XAMPP 服务器中运行,但在外部服务器中不起作用:

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home/biranchi/public_html/push.php on line 42

Failed to connect 110

警告:stream_socket_client() [function.stream-socket-client]:无法连接到 /home/biranchi/public_html/push.php 中的 ssl://gateway.sandbox.push.apple.com:2195(连接超时)在第 42 行

连接失败 110

What is the error? Do i need to change some setting in the server?

错误是什么?我需要更改服务器中的某些设置吗?

回答by palaniraja

I had fixed the issue by opening the port 2195 on the production server. You can verify by following command $telnet gateway.push.apple.com 2195

我通过在生产服务器上打开端口 2195 解决了这个问题。您可以通过以下命令验证 $ telnet gateway.push.apple.com 2195

-bash-3.2# telnet gateway.push.apple.com 2195

Trying 17.149.38.141...
Connected to gateway.push.apple.com (17.149.38.141).
Escape character is '^]'.
Connection closed by foreign host.

回答by Jeremy Morgan

Check your personal firewall settings and make sure you're not blocking this out. Try disabling the firewall.

检查您的个人防火墙设置并确保您没有阻止它。尝试禁用防火墙。

Also, some APIs like requests to come from an actual domain rather than a desktop. I don't have reason to believe Apple works this way, but that's something to check also.

此外,一些 API 喜欢来自实际域而不是桌面的请求。我没有理由相信 Apple 是这样工作的,但这也是需要检查的。

Also make sure and ping gateway.sandbox.push.apple.com and make sure you have a good connection.

还要确保 ping gateway.sandbox.push.apple.com 并确保您有良好的连接。

回答by Heitor

You have to set your firewall to allow all the 17.0.0.0/8 block (it all belongs to Apple!). Check THIS ANSWER

您必须将防火墙设置为允许所有 17.0.0.0/8 块(它都属于 Apple!)。检查这个答案

And according to Apple:

根据苹果公司的说法:

The APNs servers use load balancing, so your devices won't always connect to the same public IP address for notifications. It's best to allow access to these ports on the entire 17.0.0.0/8 address block, which is assigned to Apple.

APNs 服务器使用负载平衡,因此您的设备不会总是连接到相同的公共 IP 地址以获取通知。最好在分配给 Apple 的整个 17.0.0.0/8 地址块上允许访问这些端口。

If you are using CSF firewall (like me), I'd recommend to add this line to csf.allow file:

如果您使用 CSF 防火墙(像我一样),我建议将此行添加到 csf.allow 文件中:

tcp|out|d=2195|d=17.0.0.0/8

tcp|out|d=2195|d=17.0.0.0/8

Then restart CSF. Using the above instead of just "17.0.0.0/8" will allow only outbond connections to Apple and specifically to port 2195. NSA won't like it but this is much more precise and safe! ;)

然后重新启动CSF。使用上面的而不是“17.0.0.0/8”将只允许外连连接到 Apple,特别是端口 2195。NSA 不会喜欢它,但它更精确和安全!;)