新APNS提供的API和PHP [英] New APNS Provider API and PHP

查看:1060
本文介绍了新APNS提供的API和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始创建基于这从PHP发送推送通知

I started creating some code based upon this for sending push notifications from PHP.

不过,现在我已经知道有是利用HTTP / 2,并在响应提供反馈一个新的API,我试图找出我需要做什么来得到反馈。

However now that I have understood there is a new API which utilizes HTTP/2 and provides feedback in the response, I am trying to figure out what I need to do to get that feedback.

我一直没能找到任何教程或样品code给我方向(我猜是因为它太新了)。

I haven't been able to find any tutorials or sample code to give me direction (I guess because it is so new).

是否可以使用连接到APNS与新的供应商API的在stream_socket_client()的方法?我如何得到反馈?所有我从的fwrite($计划生育,$味精,strlen的($味精))回现在是一个数字。对于所有意图和目的,你可以考虑我的code一样从的SO质疑我根据我的 code

Is it possible to use the stream_socket_client() method of connecting to APNS with the new provider API? How do I get the feedback? All I get back from fwrite($fp, $msg, strlen($msg)) right now is a number. For all intents and purposes, you can consider my code the same as the code from the SO question I based my code on

谢谢!

推荐答案

随着新的HTTP / 2 APNS提供商的API,你可以使用卷曲发送推送通知。

With the new HTTP/2 APNS provider API, you can use curl to send push notifications.

修改

在继续(由@Madox指出的),应安装(从包preferably)的OpenSSL> = 1.0.2e。使用以下命令验证

Before proceeding (as noted by @Madox), openssl >= 1.0.2e of should be installed (from package preferably). Verify with the command

openssl version

a)您的PHP版本应该是> = 5.5.24让恒CURL_HTTP_VERSION_2_0定义。

a) Your version of PHP should be >= 5.5.24 so that the constant CURL_HTTP_VERSION_2_0 is defined.

二)请确保您有卷曲版7.46+安装在系统中以

b) Make sure that you have curl version 7.46+ installed in your system with

curl --version

C)卷曲应该有HTTP / 2的支持功能。在输出中键入previous命令时,你应该会看到像这样的一行:

c) Curl should have http/2 support enabled. In the output when typing the previous command, you should see a line like this one:

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

如果HTTP2显示不出来,你可以按照这个优秀的教程安装HTTP / 2卷曲<一个href=\"https://serversforhackers.com/video/curl-with-http2-support\">https://serversforhackers.com/video/curl-with-http2-support

if HTTP2 doesn't show up, you can follow this excellent tutorial to install http/2 for curl https://serversforhackers.com/video/curl-with-http2-support

确认卷曲检测的OpenSSL> = 1.0.2e,做卷曲--version输出应该是这样的:

Verify that curl detected openssl >= 1.0.2e, doing curl --version should output something like this:

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) Once you everything installed, you can test it in the command line:

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样本code,我已经成功尝试:
    

f) Here is a sample code in PHP that I have successfully tried:

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);

}

这篇关于新APNS提供的API和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆