无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)

时间:2013-11-08 18:01:30

标签: php ios ssl

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

5 个答案:

答案 0 :(得分:10)

此问题是Apple Push通知中的常见问题,要解决此错误,您必须按如下方式进行操作:

  1. 在服务器本地和远程测试您的pem文件,如果问题是服务器,请转到2 else build pem correct file。

  2. 在具有证书文件的文件夹上设置权限错误。这对我有用:

  3.   

    chmod 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. 
    

答案 1 :(得分:2)

如果 MAC , (内置服务器使用终端工作正常但不通过浏览器,对我来说,所以我安装了MAMP。)

1.转到---> / Library / WebServer / Documents / ----在此复制 php ckdev。pem 文件。

2转到终端 - > $ open / private / etc - >转到---> apache2>原件> httpd.config文件 - > **" #LoadModule php5_module libexec / apache2 / libphp5.so" ,删除" " ..(也许,你必须也改变了许可..!)

然后转到浏览器并检查 - >本地主机/ yourPhpFile.php

如果是 Windows系统

1.安装 WAMP

2.goto php.ini 文件--->搜索此内容" ; extension = php_openssl.dll "排队并删除分号" ; "。

3.从右下角转到WAMP图标转到 PHP> PHP Extensions>选择php_openssl ..

这就是......希望这可以帮助更多的寻求者。

答案 2 :(得分:1)

在我的情况下,问题是我忘了在PHP脚本中正确设置我的密码。

<?php

// Put your device token here (without spaces):
$deviceToken = 'xxx';

// Put your private key's passphrase here:
$passphrase = 'xxx';

答案 3 :(得分:0)

对我而言,这是SELinux问题。

因此,在/ etc / selinux / config文件中,将 SELINUX =强制执行设置为 SELINUX =已禁用。然后重启。多数民众赞成。

答案 4 :(得分:0)

我刚收到SAME错误,我找到了解决问题的方法。

‚mypassword‘代替'mypassword'

似乎记事本改变了引号。这真的花了我4个小时......希望有人会读到这个,节省一些时间......

相关问题