stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝) [英] stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)

查看:307
本文介绍了stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个php文件,用于向苹果iphone用户发送通知。它为其他服务器工作但不在我的服务器上工作。我准确地制作了.pem文件,并打开了端口号2195,2196。但它仍然不起作用。
请有人帮我解决这个问题。这是我发送推送通知的PHP代码:

I made a php file for sending notification to the apple iphone users. Its working for the other server but not working in my server. I have made the .pem file accurately and also opened the port number 2195,2196. But still its not working. Please someone help me out to get rid of this issue. Here is my php code for sending push notification:

 <?php
// Put your device token here (without spaces):
$deviceToken = 'f672c26cbfb279e45c1b66d0ddb738d8043c785d5bb8dd20a72d52ae88d4a604';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'Welcome in testing';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
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);
?>


推荐答案

我发现这篇文章时有同样的问题和我的问题实际上是两个问题,并认为它可能在这里有用。我在Ubuntu / Php 5.5

I found this article while having the same issues and my problem was actually two issues and thought it could be useful here. I am on Ubuntu / Php 5.5

prompt\#: openssl s_client -connect gateway.sandbox.push.apple.com:2195
     -cert ./push-crt.pem 
     -key ./push-key.pem 
     -CApath ./entrust_root_certification_authority.pem

当我找到上述测试时,我意识到我错过了root权限我的服务器上的证书,第1项。一旦我运行上面的程序并且它工作了,我意识到我使用的库只传递了一个文件,更多关于第二项的文件。

When I found the above test I realized I was missing the root authority certificate on my server, item 1. Once I ran the above and it worked I realized the library I was using only passed one file, more on that in item two.

在我的服务器上,我没有安装正确的根证书,因此无法验证我的PEM文件。接下来我会谈到的。一旦我找到这个页面[在哪里获取Entrust Root CA PEM文件?] [1]我检查过,我遇到了同样的问题。我假设你d / l到你的主文件夹......

On my server I did not have the correct root certificate installed and therefore could not verify my PEM file. I'll get to that next. Once I found this page [Where to get the Entrust Root CA PEM file?][1] I checked and I has the same problem. I assume you d/l to your home folder...

prompt\#: sudo cp ~/entrust_root_certification_authority.pem 
    /usr/local/share/ca-certificates/entrust_root_certification_authority.crt
prompt\#: sudo update-ca-certificates

您应该看到响应表明已安装或更新了证书。现在,我到了某个地方。我开始得到与我的PEM文件直接相关的不同连接错误。我可以使用的东西。

You should see a response indicated a certificate was installed or updated. Now, I was getting somewhere. I started to get a different connection error directly related to my PEM file. Something I could work with.

如果您使用像我这样的库传递一个文件,并且您使用证书和密钥通过了上述测试,那么您需要将密钥和证书。对我来说,使用上面的示例名称push-crt.pem

If you are using a library like mine which passes one file and you passed the above test using both your cert and key then you need to combine your key and cert. For me, using my sample names above of push-crt.pem

prompt\#: cat push-crt.pem > push.pem
prompt\#: echo >> push.pem
prompt\#: cat push-key.pem >> push.pem

然后我使用组合的密钥/证书对,一切都开始工作。

then I used the combined key/certificate pair and everything started to work.

Phew。希望它对某人有所帮助。

Phew. Hope it helps someone.

这篇关于stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆