这是一个深夜。我刚刚在 google/stackoverflow 搜索和实验中花了 10 个小时。看来我讨厌苹果推送通知。我非常沮丧,并会感谢任何帮助。
谢谢你。
问题:
用于发送 Apple 推送通知的 PHP 代码在两周前成功运行,现在停止运行并引发以下错误:
PHP Warning: stream_socket_client(): Failed to enable crypto in /home/...
PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /home/...
它停止在两个单独的服务器上工作,这些服务器使用单独的脚本进行 APNs 发送。
环境:
服务器:CentOS 6.5 和 PHP 5.4.32 和 Ubuntu 14.04.3 和 PHP 5.5.9
APN:处于生产模式
证书:经过 700 多个推送通知测试。
其中一台服务器正在使用https://github.com/immobiliare/ApnsPHP,其他 - https://github.com/antongorodezkiy/wp-apn在本地主机上我测试了简单文件而不使用任何第三方代码。
调查:
对于以下所有情况,我使用相同的活动设备令牌和相同的生产 PEM 证书。
php
但是,即使是这个简单的代码也不能在服务器和 localhost 上运行,并返回与上面相同的错误:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/absolute/path/to/apn_prod.pem');
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
我还尝试使用stream_context_set_option()
选项、包含entrust_2048_ca.cer
等,甚至本文中的一些选项。尽管提供的代码在 2015 年 8 月之前无需任何修改即可工作。
openssl
连接与 openssl (链接)一起使用:
openssl s_client -connect gateway.push.apple.com:2195 -cert /absolute/path/to/apn_prod.pem -debug -showcerts -CAfile /absolute/path/to/server-ca-cert.pem
并得到了CONNECTED(00000003)
和Verify return code: 0 ( ok )
。
远程登录
连接使用 telnet:
-sh-4.1$ telnet gateway.push.apple.com 2195
Trying 17.172.233.150...
Connected to gateway.push.apple.com.
pecl apn
它没有发送推送通知。我只是尝试使用示例代码的改编,但得到了错误Invalid token
。该令牌是活跃的,我在任何地方都使用过相同的令牌,也用于休斯顿和 Ruby。
休斯顿
它与休斯顿合作
apn push "0346a53f...231d9d6abe11" -c /absolute/path/to/apn_prod.pem -m "Hello from the command line!" -e "production"
红宝石
我不是一个 Ruby 程序员(至少),但是在休斯顿取得成功之后,我发现并改编了没有休斯顿依赖的 Ruby 代码。
它奏效了:
#!/usr/bin/env ruby
require 'openssl'
require 'socket'
require 'json'
token = "0346a53f...231d9d6abe11"
cert = File.read("/absolute/path/to/apn_prod.pem")
ctx = OpenSSL::SSL::SSLContext.new
ctx.key = OpenSSL::PKey::RSA.new(cert, '') #set passphrase here, if any
ctx.cert = OpenSSL::X509::Certificate.new(cert)
sock = TCPSocket.new('gateway.push.apple.com', 2195) #development gateway
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
ssl.connect
payload = {"aps" => {"alert" => "Oh hai!", "badge" => 1, "sound" => 'default'}}
json = payload.to_json()
token = [token.delete(' ')].pack('H*') #something like 2c0cad 01d1465 346786a9 3a07613f2 b03f0b94b6 8dde3993 d9017224 ad068d36
apnsMessage = "\0\0 #{token}\0#{json.length.chr}#{json}"
ssl.write(apnsMessage)
ssl.close
sock.close
puts "End"
问题:
- PHP有什么问题?是否存在与此问题相关的错误?(虽然我没有找到错误报告)
- 任何想法如何解决这个问题?
- 任何想法在 PHP 和 Ruby 案例中可能有什么区别(我假设 Python 或 Perl 也可以正常工作)?我什至尝试阅读 PHP 源代码,但未能成功理解如何
stream_socket_client()
实现。