借助新的 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 没有显示,你可以按照这个优秀的教程安装 http/2 for curl 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);
}