通过 PHP 发送 Apple 推送通知:无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接超时)

发布于 2025-01-16 05:56:23 字数 1916 浏览 3 评论 0原文

我有以下 PHP 代码来将 Apple 推送通知发送到我的应用程序:

<?php
$body = '{"aps":{"alert":{"title":"test title","subtitle":"","body":"test body"},"badge":0,"sound":"default","additional_data":"test additional_data"}}';

$context = stream_context_create();
stream_context_set_option($context, "ssl", "local_cert", "certificate.pem");
stream_context_set_option($context, "ssl", "passphrase", "HERE_COMES_THE_PASSWORD_OF_THE_certificate.pem_FILE");
$socket = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $context);
$msg = chr(0) . chr(0) . chr(32) . pack("H*", "HERE_COMES_MY_APPLE_PUSH_TOKEN") . pack("n", strlen($body)) . $body;
$result = fwrite($socket, $msg, strlen($msg));
fclose($socket);
?>

此代码与名为 certificate.pem 的证书文件一起存储在我的服务器上。

我自一个月以来一直使用此代码,没有任何问题。今天,我注意到我不再收到推送通知。

PHP错误日志显示以下内容:

[23-Mar-2022 11:39:45 UTC] PHP Warning:  stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home2/kd37875/public_html/test/index.php on line 7
[23-Mar-2022 11:39:45 UTC] PHP Warning:  fwrite() expects parameter 1 to be resource, bool given in /home2/kd37875/public_html/test/index.php on line 9
[23-Mar-2022 11:39:45 UTC] PHP Warning:  fclose() expects parameter 1 to be resource, bool given in /home2/kd37875/public_html/test/index.php on line 10

首先,我认为证书文件有问题。然后,我找到了这个:https://www.pushtry.com网站。如果我插入设备令牌、上传certificate.pem 文件、插入捆绑包ID 和一条消息,我就可以在我的应用程序中成功接收推送消息。 (网站上有一个密码字段,但如果我不插入它,它也可以工作。不知道为什么。)所以这告诉我,certificate.pem 一定没问题。

屏幕截图https://www.pushtry.com

你知道我做错了什么吗?为什么它不再起作用了?苹果改变了什么吗?

I have the following PHP code to send Apple Push Notifications to my app:

<?php
$body = '{"aps":{"alert":{"title":"test title","subtitle":"","body":"test body"},"badge":0,"sound":"default","additional_data":"test additional_data"}}';

$context = stream_context_create();
stream_context_set_option($context, "ssl", "local_cert", "certificate.pem");
stream_context_set_option($context, "ssl", "passphrase", "HERE_COMES_THE_PASSWORD_OF_THE_certificate.pem_FILE");
$socket = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $context);
$msg = chr(0) . chr(0) . chr(32) . pack("H*", "HERE_COMES_MY_APPLE_PUSH_TOKEN") . pack("n", strlen($body)) . $body;
$result = fwrite($socket, $msg, strlen($msg));
fclose($socket);
?>

This code is stored on my server together with the certificate file called certificate.pem.

I'm using this code unchanged since month without any problems. Today, I noticed, that I'm not getting push notifications any more.

The PHP error log shows the following:

[23-Mar-2022 11:39:45 UTC] PHP Warning:  stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home2/kd37875/public_html/test/index.php on line 7
[23-Mar-2022 11:39:45 UTC] PHP Warning:  fwrite() expects parameter 1 to be resource, bool given in /home2/kd37875/public_html/test/index.php on line 9
[23-Mar-2022 11:39:45 UTC] PHP Warning:  fclose() expects parameter 1 to be resource, bool given in /home2/kd37875/public_html/test/index.php on line 10

First, I thought, there's something wrong with the certificate file. Then, I found this: https://www.pushtry.com website. If I insert the Device Token, upload the certificate.pem file, insert the Bundle ID and a message, I'm successfully receiving a push message in my app. (There's a field for password on the website but it's also working if I don't insert it. No idea, why.) So this says me, that the certificate.pem must be okay.

screenshot from https://www.pushtry.com

Do you have any idea what I'm doing wrong? Why doesn't it work any more? Did Apple change something?

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

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

发布评论

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

评论(1

梦明 2025-01-23 05:56:24

我终于解决了我的问题。苹果大约一年前对此做出了改变。不知道为什么它今天才影响到我。

这是工作代码:

<?php
function sendHTTP2Push($http2_server, $apple_cert, $app_bundle_id, $message, $token) {
if(!defined('CURL_HTTP_VERSION_2_0')) {
    define('CURL_HTTP_VERSION_2_0', 3);
}
$http2ch = curl_init();
curl_setopt($http2ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);

curl_setopt_array($http2ch, array(
    CURLOPT_URL => "{$http2_server}/3/device/{$token}",
    CURLOPT_PORT => 443,
    CURLOPT_HTTPHEADER => array("apns-topic: {$app_bundle_id}"),
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => $message,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSLCERT => realpath($apple_cert),
    CURLOPT_HEADER => 1
));

$result = curl_exec($http2ch);
if($result === FALSE) {
    throw new Exception('Curl failed with error: ' . curl_error($http2ch));
}

$status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);
return $status;

curl_close($http2ch);
}

$status = sendHTTP2Push('https://api.development.push.apple.com', 'certificate.pem', 'HERE_COMES_THE_APPS_BUNDLE_ID', '{"aps":{"alert":{"title":"test title","subtitle":"","body":"test body"},"badge":0,"sound":"default","additional_data":"test additional_data"}}', 'HERE_COMES_MY_APPLE_PUSH_TOKEN');

echo "Response code: ".$status;
?>

来源:https://gist.github.com/valfer/18e1052bd4b160fed86e6cbb426bb9fc

I finally solved my problem. Apple made a change to this about a year ago. No idea why it affected me only today.

This is the working code:

<?php
function sendHTTP2Push($http2_server, $apple_cert, $app_bundle_id, $message, $token) {
if(!defined('CURL_HTTP_VERSION_2_0')) {
    define('CURL_HTTP_VERSION_2_0', 3);
}
$http2ch = curl_init();
curl_setopt($http2ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);

curl_setopt_array($http2ch, array(
    CURLOPT_URL => "{$http2_server}/3/device/{$token}",
    CURLOPT_PORT => 443,
    CURLOPT_HTTPHEADER => array("apns-topic: {$app_bundle_id}"),
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => $message,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSLCERT => realpath($apple_cert),
    CURLOPT_HEADER => 1
));

$result = curl_exec($http2ch);
if($result === FALSE) {
    throw new Exception('Curl failed with error: ' . curl_error($http2ch));
}

$status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);
return $status;

curl_close($http2ch);
}

$status = sendHTTP2Push('https://api.development.push.apple.com', 'certificate.pem', 'HERE_COMES_THE_APPS_BUNDLE_ID', '{"aps":{"alert":{"title":"test title","subtitle":"","body":"test body"},"badge":0,"sound":"default","additional_data":"test additional_data"}}', 'HERE_COMES_MY_APPLE_PUSH_TOKEN');

echo "Response code: ".$status;
?>

Source: https://gist.github.com/valfer/18e1052bd4b160fed86e6cbb426bb9fc

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