gpt4 book ai didi

php - 新的 APNS 提供商 API 和 PHP

转载 作者:IT王子 更新时间:2023-10-29 00:05:46 26 4
gpt4 key购买 nike

我开始根据 this 创建一些代码用于从 PHP 发送推送通知。

但是,现在我了解到有一个新的 API 使用 HTTP/2 并在响应中提供反馈,我正试图弄清楚我需要做什么才能获得该反馈。

我找不到任何教程或示例代码来指导我(我猜是因为它太新了)。

是否可以使用 stream_socket_client() 方法通过新的提供程序 API 连接到 APNS?我如何获得反馈?我现在从 fwrite($fp, $msg, strlen($msg)) 得到的只是一个数字。出于所有意图和目的,您可以认为我的代码与 the SO question I based my code on 中的代码相同

谢谢!

最佳答案

借助新的 HTTP/2 APNS 提供程序 API,您可以使用 curl 发送推送通知。

编辑

在继续之前(如@Madox 所述),应该安装 openssl >= 1.0.2e(最好从包中安装)。用命令验证

openssl version

a) 您的 PHP 版本应该 >= 5.5.24 以便定义常量 CURL_HTTP_VERSION_2_0。

b) 确保您的系统中安装了 curl 版本 7.46+

curl --version

c) Curl 应该启用 http/2 支持。在键入上一个命令时的输出中,您应该看到这样一行:

Features: IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets 

如果 HTTP2 没有出现,您可以按照这个优秀的教程为 curl 安装 http/2 https://serversforhackers.com/video/curl-with-http2-support

验证 curl 检测到 openssl >= 1.0.2e,执行 curl --version 应该输出如下内容:

curl 7.47.1 (x86_64-pc-linux-gnu) libcurl/7.47.1 OpenSSL/1.0.2f zlib/1.2.8 libidn/1.28 nghttp2/1.8.0-DEV librtmp/2.3

e) 安装完所有内容后,您可以在命令行中对其进行测试:

curl -d '{"aps":{"alert":"hi","sound":"default"}}' \ 
--cert <your-certificate.pem>:<certificate-password> \
-H "apns-topic: <your-app-bundle-id>" \
--http2 \
https://api.development.push.apple.com/3/device/<device-token>

f) 这是我已经成功尝试过的 PHP 示例代码:

if(defined('CURL_HTTP_VERSION_2_0')){

$device_token = '...';
$pem_file = 'path to your pem file';
$pem_secret = 'your pem secret';
$apns_topic = 'your apns topic. Can be your app bundle ID';


$sample_alert = '{"aps":{"alert":"hi","sound":"default"}}';
$url = "https://api.development.push.apple.com/3/device/$device_token";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

//On successful response you should get true in the response and a status code of 200
//A list of responses and status codes is available at
//https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

var_dump($response);
var_dump($httpcode);

}

关于php - 新的 APNS 提供商 API 和 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34684099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com