在过去的几天里,当我们使用套接字连接到生产服务器上的 APN 服务器时,我们遇到了一些奇怪的 PHP 行为。
大多数情况下,有效负载的推送不会出现任何错误,并且客户端会收到通知。然而,在某些情况下,我们开始收到 PHP 错误(即使我们收到错误,有时也会推送通知)。当我们开始看到这个错误时,它会持续几个小时然后消失,PHP 继续工作,就像什么都没发生一样。
另一个奇怪的事情是,从 shell 运行相同的 PHP 代码不会产生任何错误。从 web (nginx / php-fpm) 运行它确实...在 shell 和 web 上运行的 PHP 具有相同的配置并共享相同的 php.ini。唯一的区别是 web 运行在 php-fpm 上。
此外,相同的代码+证书在我们的临时服务器上运行没有任何错误。生产服务器是登台服务器的副本,因此每个配置都是相同的。
我们能够找到一些可能导致此错误的答案,包括来自 stackoverflow.com 的答案,但我们无法找到解决方案或解决它。
向 Apple 服务器发送的通知是逐个发送的,而不是捆绑发送的。但我们并没有建立太多的联系(也许每天一千个)。没有排队系统。
所以,简而言之
- 我们有时会在发送通知时收到 PHP 错误,但并非总是如此。
- 通过相同的 PHP 从 shell 发送通知不会产生任何错误
- 从登台服务器发送通知不会产生任何错误
我们尝试了这些
- 重新创建证书和密钥
- 重新创建 PEM 文件
- 将 ssl:// 更改为 sslv3://
- 使用stream_socket_client
- 使用 fsockopen
- 更改/删除证书密码
错误是:
2012/08/28 12:18:09 [error] 4282#0: *225858 FastCGI sent in stderr:
"PHP message: PHP Warning: fwrite() [<a href='function.fwrite'>function.fwrite</a>]:
SSL operation failed with code 1. OpenSSL Error messages:
error:1409F07F:SSL routines:func(159):reason(127) in
/usr/local/nginx/html/play/classes/PushNotification.php on line 283"
while reading response header from upstream, client: 94.---.---.---,
server: play.--------.com, request: "POST /game_request_random.php HTTP/1.1",
upstream: "fastcgi://unix:/var/run/phpfpm.sock:",
host: "play.--------.com", referrer: "http://--------.com/"
从 php 连接和发送有效负载的代码实际上是类的一部分,这部分是建立连接并发送有效负载的:
private function ConnectAndSend ( $msg = false ) {
$ctx = stream_context_create();
stream_context_set_option( $ctx, 'ssl', 'local_cert', $this->certificate );
stream_context_set_option( $ctx, 'ssl', 'passphrase', $this->certificatepass );
// Open a connection to the APNS server
$fp = stream_socket_client( APN_SERVER, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx );
if ( !$fp ) {
errorlog( "Push notification error : $err $errstr" );
$this->error = "$err $errstr";
return;
}
// Build the notification
if ( !$msg ) {
$msg = chr( 0 ) . pack( 'n', 32 ) . pack( 'H*', $this->devicetoken ) . pack( 'n', strlen( $this->payload ) ) . $this->payload;
}
// Send it to the server
if ( !($result = fwrite( $fp, $msg, strlen( $msg ) )) ) {
// Could not send
$this->error = 'Notification could not be send';
errorlog( "Push notification error : {$this->error}" );
} else {
// Notification sent
$this->error = false;
errorlog( "Push notification sent" );
}
fclose($fp);
// Reset the content
$this->devicetoken = false;
$this->message = false;
$this->command = false;
$this->badge = 0;
$this->payload = false;
$this->sound = false;
}
- Stream_socket_connection 是错误消息中出现的第 283 行
- 我们没有使用沙箱(sslv3://gateway.push.apple.com:2195)
- PHP版本是5.3.15
这是我们不知道的 PHP 或 OpenSSL 错误吗?有什么想法以及在哪里检查吗?
Apple 是否有一个网站可以让我们检查 APN 网络当前的运行状况?
任何帮助是极大的赞赏...
谢谢