使用 APNS PHP 获取错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1898300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:13:28  来源:igfitidea点击:

Getting errors using APNS PHP

phppush-notificationapple-push-notifications

提问by Bryan

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

警告: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]: 无法连接到 ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/bryan/sendpush.php on line 14

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

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

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

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

警告:stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL 错误信息:error:14094410:SSLroutines:SSL3_READ_BYTES:sslv3 alert handshake failure in /home/bryan/PushService.php on line 27

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

警告:stream_socket_client() [function.stream-socket-client]: 无法连接到 ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/bryan/PushService.php on line

回答by Daniel W.

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

  1. 使用私钥的绝对路径而不是相对路径。

  2. 确保允许php 用户(或网络服务器用户,取决于.. www-data, apache, nginx, www...)阅读它(chown, chmod)。

回答by Genki

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