那这篇文章呢?
自去年12月以来,用于iOS上的推送通知的新API(APNs Provider API)已经可用!
APNs提供程序API:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html
与执行套接字通信的复杂旧API不同,它基于HTTP / 2,非常简单。
很高兴能够轻松获得传输结果,因此我在Mac(OS X El Capitan)上使用PHP进行了尝试,尽管已经很晚了。
HTTP / 2环境维护
首先,需要以下环境才能使用PHP实现HTTP2请求。
- openssl 1.0.2e及更高版本
- curl 7.46或更高(nghttp2)
- PHP 5.5.24及以上
openssl安装
Mac上预装的openssl版本较旧,因此请与brew安装。
1 | $ brew install openssl |
接下来,将链接粘贴到您在brew中放入的openssl。
1 | $ brew link openssl --force |
检查版本,如果没有问题,可以!
1 2 3 4 | $ which openssl /usr/local/bin/openssl $ openssl version OpenSSL 1.0.2f 28 Jan 2016 |
卷曲安装
这也是一个旧版本,请与brew安装。
一起安装nghttp2!
1 | $ brew install curl --with-nghttp2 --with-openssl |
不要忘记也将链接放在这里。
1 | $ brew link curl --force |
检查版本,如果看起来如下所示,就可以了!
确认"功能"中没有
1 2 3 4 5 6 | $ which curl /usr/local/bin/curl $ curl --version curl 7.47.1 (x86_64-apple-darwin15.0.0) libcurl/7.47.1 OpenSSL/1.0.2f zlib/1.2.5 nghttp2/1.7.1 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets |
PHP安装
我认为很多人已经安装了PHP,但是重新安装它可能不会让人上瘾。
1 2 | $ brew install --with-homebrew-curl --with-homebrew-openssl --without-snmp php56 $ brew install php56-mcrypt |
用
phpinfo,
检查
1 | $ php -r 'phpinfo();' |
如果显示这样的结果,就可以了!
1 2 3 4 5 6 7 8 9 10 11 12 | PHP Version => 5.6.18 (省略) curl cURL support => enabled cURL Information => 7.47.1 (省略) openssl OpenSSL support => enabled OpenSSL Library Version => OpenSSL 1.0.2f 28 Jan 2016 (省略) |
PHP脚本
如果您来这里,这是一个轻松的胜利。
new_api.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php if(defined('CURL_HTTP_VERSION_2_0')){ $device_token = 'your 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'; $alert = '{"aps":{"alert":"Hello, Nogizaka46","sound":"default"}}'; $url = "https://api.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); var_dump($response); var_dump($httpcode); // デバッグ用 // $info = curl_getinfo($ch); // $errno = curl_errno($ch); // $error = curl_error($ch); // var_dump($info); // var_dump($errno); // var_dump($error); } |
运行此PHP脚本,如果状态代码为200,就可以了!
如果发生错误,将返回类似
印象数
创建支持HTTP / 2的环境时遇到了一些麻烦,但是实现和错误处理变得更加容易,并且由于它是现代API,因此从现在开始我将积极使用它。
参考网站
新的APNS Provider API和PHP:
http://stackoverflow.com/questions/34684099/new-apns-provider-api-and-php/34955920
从(1.6)开始处理APNs Provider API(HTTP / 2):
http://soh335.hatenablog.com/entry/2015/12/22/222041
发送带有curl扩展名的HTTP / 2请求:
http://qiita.com/masakielastic/items/f563437c44b0d4c04f87
从Mac上的PHP使用cURL访问https时,失败:
http://takemaru123.hatenablog.jp/entry/2015/05/22/123753