本周我尝试使用php套接字,但有一件事似乎不起作用.
我想从套接字服务器发送Apple推送通知(apns).套接字服务器工作正常但打开另一个套接字向apns发送消息不起作用.
我使用的服务器套接字脚本类似于php.net的示例.(http://www.php.net/manual/en/sockets.examples.php)我用来发送apns通知的脚本是以下功能:
function sendToAPNS($data, $deviceTokens, $pem, $password, $debug=0) { $server = ($debug == 1) ? 'gateway.sandbox.push.apple.com' : 'gateway.push.apple.com'; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/testing/pushNotifications/'.$pem); stream_context_set_option($ctx, 'ssl', 'passphrase', $password); $fp = stream_socket_client('ssl://'. $server .':2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) { echo 'APNS error:'. $err .' '. $errstr ."\n"; return; } $body['aps'] = array('alert' => $data['message']); foreach($data AS $key => $value) { if($key != 'message') $body['aps'][$key] = $value; } $payload = json_encode($body); for($i = 0,$max=count($deviceTokens); $i < $max; $i++) { $msg = chr(0) . pack('n', 32) . pack('H*', $deviceTokens[$i]) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $msg, strlen($msg)); if (!$result) BPLog('Message['. $data['ID'] .'] not delivered to '. $deviceTokens[$i] .': '. PHP_EOL); // ob_flush(); // flush(); } fclose($fp); return true; }
显示以下警告:
PHP Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:func(148):reason(1040) in /home/testing/pushNotifications/index.php on line 162 Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:func(148):reason(1040) in /home/testing/pushNotifications/index.php on line 162 PHP Warning: stream_socket_client(): Failed to enable crypto in /home/testing/pushNotifications/index.php on line 162 Warning: stream_socket_client(): Failed to enable crypto in /home/testing/pushNotifications/index.php on line 162 PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /home/testing/pushNotifications/index.php on line 162 Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /home/testing/pushNotifications/index.php on line 162
但是该功能在其他环境中工作.我已经在一个php文件中对它进行了测试,该文件可以从浏览器中调用,它会像它应该的那样发送.我使用证书的绝对路径,这不是问题.
也许在php中存在差异,因为从命令行调用套接字服务器.或者也许根本无法从套接字服务器中打开另一个套接字流?
有谁知道问题可能是什么?