APNS + PHP“stream_socket_client(): 无法启用加密”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28995197/
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
APNS + PHP "stream_socket_client(): Failed to enable crypto"
提问by Samer
I'm having trouble with using APNS with PHP and getting the following message:
我在将 APNS 与 PHP 结合使用时遇到问题并收到以下消息:
stream_socket_client(): Failed to enable crypto
The problem only happens sometimes, and other times it would actually send the push.
问题只是偶尔发生,而其他时候它实际上会发送推送。
Since I have the test script on a loop of 10 iterations, I would sometimes get this:
由于我有 10 次迭代循环的测试脚本,我有时会得到这个:
stream_socket_client(): SSL: Connection reset by peer
I'm testing using the sandbox server tls://gateway.sandbox.push.apple.com:2195
我正在使用沙箱服务器进行测试 tls://gateway.sandbox.push.apple.com:2195
Here is what I tried:
这是我尝试过的:
- I tried to reissue the PEM and all certificates with it.
- I played around with the request protocol
sslv3://
andtls://
. - I played around with the passphrase (push worked without the passphrase btw)
- I tried searching stackoverflow for a solution and nothing worked.
- Checked pem file permissions
644
- Checked pem parent directories permissions
755
- 我尝试重新颁发 PEM 和所有证书。
- 我玩弄了请求协议
sslv3://
和tls://
. - 我玩弄了密码短语(顺便说一句,没有密码短语的推送工作)
- 我尝试在 stackoverflow 中搜索解决方案,但没有任何效果。
- 检查pem文件权限
644
- 检查 pem 父目录权限
755
It seems that all the solutions I found on Google and SO are people having problem pushing altogether.
似乎我在 Google 和 SO 上找到的所有解决方案都是完全有问题的人。
I feel like the service is rate limited maybe? Because we waited a while (around 15 minutes) and then tried it again, and was able to successfully push around 100 messages until I started getting that message again.
我觉得服务可能是限速的?因为我们等了一段时间(大约 15 分钟)然后再次尝试,并且能够成功推送大约 100 条消息,直到我再次开始收到该消息。
采纳答案by greg_diesel
The sandbox push service is rate limited. I have experienced this myself when testing but have never encountered any such limit using the production API.
沙盒推送服务是限速的。我自己在测试时也遇到过这种情况,但在使用生产 API 时从未遇到过任何此类限制。
You might also be hitting their other protections.
您可能还会遇到他们的其他保护措施。
Are you opening a connection, sending a message, closing connection and then looping and doing it all over again?
你是打开一个连接,发送一条消息,关闭连接,然后循环再做一遍吗?
That will get your notifications dropped. Apple wants you to send several push notifications using the same connection, not a new one each time.
这将使您的通知下降。Apple 希望您使用同一个连接发送多条推送通知,而不是每次都发送一条新通知。
Best Practices for Managing Connections
You may establish multiple connections to the same gateway or to multiple gateway instances. If you need to send a large number of remote notifications, spread them out over connections to several different gateways. This improves performance compared to using a single connection: it lets you send the remote notifications faster, and it lets APNs deliver them faster.
Keep your connections with APNs open across multiple notifications; don't repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack.You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.
管理连接的最佳实践
您可以建立到同一个网关或多个网关实例的多个连接。如果您需要发送大量远程通知,请将它们分散到多个不同网关的连接上。与使用单个连接相比,这提高了性能:它可以让您更快地发送远程通知,并且可以让 APN 更快地传递它们。
在多个通知中保持与 APNs 的连接; 不要反复打开和关闭连接。APNs 将快速连接和断开连接视为拒绝服务攻击。除非您知道某个连接将长时间处于空闲状态,否则您应该保持连接处于打开状态——例如,如果您每天只向用户发送一次通知,则可以每天使用一个新连接。
From Apple Docs @ https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
来自 Apple Docs @ https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
回答by Roozbeh Zabihollahi
My PHP code was generating following error:
我的 PHP 代码生成以下错误:
PHP Warning: stream_socket_client(): Failed to enable crypto in /private/tmp/t.php on line 12
PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /private/tmp/t.php on line 12
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /private/tmp/t.php on line 24
The problem was, the damn certificate, expired the day before yesterday! :-) Can you believe this?
问题是,该死的证书,前天过期了!:-) 你能相信吗?
So, I need to recreate my PEM file.
所以,我需要重新创建我的 PEM 文件。
回答by Emiliano
It is not necessary recreate your pem file
没有必要重新创建您的 pem 文件
that error happens when you use an incorrect PassPhrase
当您使用不正确的 PassPhrase 时会发生该错误
regards Emiliano
问候埃米利亚诺
回答by Toxic Brain
I had this problem. Disappeared after giving write permission for 'everyone' for the .pem file.
我有这个问题。在授予 .pem 文件的“所有人”写入权限后消失。
回答by spybart
I have this problem because I foolishly forgot to include the file extension (.pem) when supplying the file path for local_cert.
我有这个问题,因为我在为 local_cert 提供文件路径时愚蠢地忘记了包含文件扩展名 (.pem)。
回答by Bhavin Rana
few checks:
几项检查:
- device token should be - with out
spaces
and with out<
or>
- make sure the path of certificate is correct and expired date of it.
- make sure the passphrase you are using is the one u used to make certificate
- 设备令牌应该是 - with out
spaces
and with out<
or>
- 确保证书路径正确且过期日期正确。
- 确保您使用的密码是您用来制作证书的密码
回答by stackOverFlew
In my case, the issue was with my mac (OSX Sierra). I uploaded php and cert to my server, ran it, and the notification was delivered.
就我而言,问题出在我的 mac (OSX Sierra) 上。我将 php 和 cert 上传到我的服务器,运行它,并发送了通知。
回答by Дмитрий Гришин
I tried examples from book of Marin Todorov iOs 6 by Tutorials.
And before I could send push notifications for automatical update I had a lot of headache cause of handshake error - stream_socket_client(): Failed to enable crypto
.
我尝试了 Tutorials 的 Marin Todorov iOs 6 书中的示例。在我可以发送自动更新的推送通知之前,我有很多令人头痛的握手错误 - stream_socket_client(): Failed to enable crypto
.
I did all of what I found in Stackoverflow - changed permissions on certificate and others.
我做了我在 Stackoverflow 中找到的所有内容 - 更改了证书和其他权限。
What I did eventually?
我最终做了什么?
I created selfsigned SSL certificate and setup Apache for serving SSL.
Also I changed SSL protocol from ssl to tls in hostname:
我创建了自签名 SSL 证书并设置了 Apache 以提供 SSL。
此外,我将主机名中的 SSL 协议从 ssl 更改为 tls:
tls://gateway.push.apple.com:2195
After that service works.
在该服务工作之后。