关于php:stream_socket_client无法连接(连接超时)

stream_socket_client unable to connect (connection timed out)

我正在使用证书和私钥

1
2
3
4
$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);

它在我的本地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

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


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

1
2
3
4
5
6
-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.


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

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

另外,请确保并ping gateway.sandbox.push.apple.com并确保您连接良好。


您必须将防火墙设置为允许所有17.0.0.0/8块(全部属于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.

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

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

然后重新启动CSF。使用上述内容而不是仅使用" 17.0.0.0/8"将仅允许到Apple(特别是端口2195)的外向连接。NSA不会喜欢它,但这更加精确和安全! ;)