我开始创建一些基于此的代码,用于从PHP发送推送通知.
但是现在我已经知道有一个新的API利用HTTP/2并在响应中提供反馈,我试图弄清楚我需要做些什么来获得反馈.
我无法找到任何教程或示例代码来指导我(我猜因为它是如此新的).
是否可以使用stream_socket_client()
与新提供者API连接到APNS 的方法?我如何获得反馈?我fwrite($fp, $msg, strlen($msg))
现在回来的只是一个数字.出于所有意图和目的,您可以认为我的代码与基于我的代码的SO问题中的代码相同
谢谢!
使用新的HTTP/2 APNS提供程序API,您可以使用curl发送推送通知.
编辑
在继续之前(如@Madox所述),应安装openssl> = 1.0.2e(最好从包中).使用命令验证
openssl version
a)您的PHP版本应该> = 5.5.24,以便定义常量CURL_HTTP_VERSION_2_0.
b)确保您的系统中安装了卷曲版本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: \ -H "apns-topic: " \ --http2 \ https://api.development.push.apple.com/3/device/
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); }