APN php 代码给出警告:stream_socket_client() [function.stream-socket-client]:无法连接到 ssl://gateway.sandbox.push.apple.com:2195 [英] APN php code giving Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195

查看:30
本文介绍了APN php 代码给出警告:stream_socket_client() [function.stream-socket-client]:无法连接到 ssl://gateway.sandbox.push.apple.com:2195的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 php 代码实现 Apple 推送通知.这是我的代码

I am trying to implement Apple Push Notification using php code. Here's my code

$deviceToken = 'My device token';
$passphrase = '';
$message = 'My first push notification!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev-cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 120, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

fclose($fp);

证书 .pem 文件与文件位于同一目录中.这段代码在我的本地机器上运行良好.我正在使用 MAMP.我的设备收到通知.

The certificate .pem file is in the same directory as the file is. This code is running fine on my local machine. I am using MAMP. I am getting notification on my devices.

但是当我在服务器上尝试时,它不工作并出现错误.

But when I am trying it on the server, it is not working and giving an error.

警告:stream_socket_client() [function.stream-socket-client]:无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)在/home/nextgen/public_html/ApplicationGenerator/appointmentportal/iosapp/SimplePush/simplepush.php第 14 行连接失败:111 连接被拒绝

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/nextgen/public_html/ApplicationGenerator/appointmentportal/iosapp/SimplePush/simplepush.php on line 14 Failed to connect: 111 Connection refused

如果证书文件有误,如何在我的本地服务器上运行?

If the certificate file is wrong, how would it work on my local server?

我没有办法摆脱这个.你们能帮我吗?

I am not getting any way out of this. Can you guys help me?

推荐答案

假设您的服务器打开了正确的端口,连接被拒绝"通常会提示无效的 .PEM 文件或不正确的密码.确保打开 .PEM 文件时,标题如下所示:

Assuming your server has the right ports open, "Connection Refused" usually hints at an invalid .PEM file or an incorrect passphrase. Make sure that when you open the .PEM file, the header looks something like this :

Bag Attributes
friendlyName: Apple Development IOS Push Services: com.yourapp.app
localKeyID: A8 77 BC 0C 2E 81 10 6E 78 9F XX XX XX XX XX XX

subject=/UID=com.yourapp.app/CN=苹果开发IOS推送服务:com.yourapp.app/C=FRissuer=/C=US/O=Apple Inc./OU=Apple Worldwide Developer Relations/CN=Apple Worldwide Developer Relations 认证机构

subject=/UID=com.yourapp.app/CN=Apple Development IOS Push Services: com.yourapp.app/C=FR issuer=/C=US/O=Apple Inc./OU=Apple Worldwide Developer Relations/CN=Apple Worldwide Developer Relations Certification Authority

后跟一个密钥,然后应该是一个标题,看起来像这样的私钥:

followed by a key which should then be followed by a header that looks like this for your private key :

Bag Attributes
friendlyName: Joe Black
localKeyID: A8 77 BC 0C 2E 81 10 6E 78 9F XX XX XX XX XX XX XX XX
Key Attributes: <No Attributes>

我建议您也删除密码以减少潜在的错误来源.

I suggest you also remove the passphrase to reduce the potential error sources.

这篇关于APN php 代码给出警告:stream_socket_client() [function.stream-socket-client]:无法连接到 ssl://gateway.sandbox.push.apple.com:2195的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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