使用 APNS PHP 出现错误

发布于 2024-08-14 12:39:48 字数 2048 浏览 3 评论 0原文

这是我正在使用的代码

<?php
$deviceToken = 'my device key';  // not putting in for security

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

socket_close($apns);
fclose($apns);
?>

,我收到这些错误

警告:stream_socket_client() [function.stream-socket-client]:无法在第 14 行 /home/bryan/sendpush.php 中设置私钥文件 `apns-dev.pem'

警告:stream_socket_client() [function.stream-socket-client]:无法在第 14 行的 /home/bryan/sendpush.php 中创建 SSL 句柄

警告:stream_socket_client() [function.stream-socket-client]:无法在第 14 行 /home/bryan/sendpush.php 中启用加密

警告:stream_socket_client() [function.stream-socket-client]:无法连接到 /home/bryan/sendpush.php 中的 ssl://gateway.sandbox.push.apple.com:2195 (未知错误)第 14 行

警告:fwrite():提供的参数不是 /home/bryan/sendpush.php 第 17 行中的有效流资源

警告:socket_close() 期望参数 1 为资源,布尔值在 /home/bryan/sendpush.php 第 19 行给出

警告:fclose():提供的参数不是 /home/bryan/sendpush.php 第 20 行中的有效流资源

我实际上现在将其归结为这些错误

警告:stream_socket_client() [function.stream-socket-client]:SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14094410:SSL 例程:SSL3_READ_BYTES:/home/bryan/PushService 中的 sslv3 警报握手失败。 php 第 27 行

警告:stream_socket_client() [function.stream-socket-client]:无法在第 27 行 /home/bryan/PushService.php 中启用加密

警告:stream_socket_client() [function.stream-socket-client]:无法连接到 /home/bryan/PushService.php 中的 ssl://gateway.sandbox.push.apple.com:2195 (未知错误)上线

Here's the code I'm using

<?php
$deviceToken = 'my device key';  // not putting in for security

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

socket_close($apns);
fclose($apns);
?>

and I get these errors

Warning: stream_socket_client() [function.stream-socket-client]: Unable to set private key file `apns-dev.pem' in /home/bryan/sendpush.php on line 14

Warning: stream_socket_client() [function.stream-socket-client]: failed to create an SSL handle in /home/bryan/sendpush.php on line 14

Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /home/bryan/sendpush.php on line 14

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/bryan/sendpush.php on line 14

Warning: fwrite(): supplied argument is not a valid stream resource in /home/bryan/sendpush.php on line 17

Warning: socket_close() expects parameter 1 to be resource, boolean given in /home/bryan/sendpush.php on line 19

Warning: fclose(): supplied argument is not a valid stream resource in /home/bryan/sendpush.php on line 20

I actually now got it down to these errors

Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /home/bryan/PushService.php on line 27

Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /home/bryan/PushService.php on line 27

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/bryan/PushService.php on line

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-08-21 12:39:48
  1. 使用私钥的绝对路径而不是相对路径。

  2. 确保 php 用户(或 Web 服务器用户,具体取决于.. www-dataapachenginxwww< /code>...) 被允许读取它 (chown, chmod)。

  1. Use the absolute path for the private key instead of relative path.

  2. Make sure the php user (or webserver user, depending.. www-data, apache, nginx, www...) is allowed to read it (chown, chmod).

梦幻之岛 2024-08-21 12:39:48

我也收到这个错误。发现我对包含证书文件的文件夹的权限设置错误。这对我有用:

chmod 755 your_folder_that_has_certificate_files

I was getting this error also. found out that I had set up the permission wrong on the folder that had the certificate file. This worked for me:

chmod 755 your_folder_that_has_certificate_files
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文