我有一个问题,我想使用 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 文件,并且它发送通知没有任何问题。我试图为我的服务器打开防火墙上的端口,因为我读到这可能会导致此问题,但仍然出现相同的错误消息。还有什么我可以做的吗?任何帮助将不胜感激,谢谢!