我有一个问题,我想用php发送推送通知,但是我一直收到这个错误:
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/colupon/public_html/iPhone/push/index.php on line 21
Failed to connect: 111 Connection refused我的代码如下:
$deviceToken = '0f************************************************************78';
$passphrase = '************';
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
$filename = 'ckdev.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', $filename);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) .
$payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered'.PHP_EOL;
// Close the connection to the server
fclose($fp);
?>我认为问题出在我使用的服务器上,因为我在另一台服务器上发布了相同的代码和相同的.pem文件,它发送通知时没有任何问题。我试图在防火墙上为我的服务器打开端口,因为我读到这可能会导致这个问题,但仍然显示相同的错误消息。还有什么我能做的吗?如有任何帮助,将不胜感激,谢谢!
发布于 2014-02-19 20:14:14
这个问题是苹果推送通知中常见的问题,要解决这个问题,你需要经历如下步骤:
通道模块755 your_folder_that_has_certificate_files
3.检查apns端口2195与托管服务器的连通性,如下所示:
跑
telnet gateway.push.apple.com 2195如果这就是问题所在
Trying 17.172.233.36...
telnet: connect to address 17.172.233.36: Connection refused您可以通过在生产服务器上打开端口2195来解决此问题。您可以通过以下命令$telnet gateway.push.apple.com 2195进行验证
-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. https://stackoverflow.com/questions/19865598
复制相似问题