Aurora Push Jpush (v3) server PHP version integration (V3 version only calls the Push API)

Source: Internet
Author: User
Tags http post php server

Because of the version upgrade, the Aurora Push API also has the V3, the function is also richer, but for our users, we still need to call the push API just enough.

Downloaded a PHP Server SDK (: http://docs.jpush.io/resources/#sdk_1), and then I saw ... We just need to push, study this, spend a little too long! So on the internet collected to see if there is a class to integrate a call, this special thanks to the dodobook of a buddy son, refer to his I small change a bit ok! (His blog http://www.dodobook.net/php/780)

Here specifically to remind you that the test is not successful remember to look at the management platform above the statistics, because before the apns_production parameters set to the production environment, the call interface success, but no user, later changed to the development environment, on the success.

Here is the code area:

<?Phpclass Jpush_send extendsMy_model {private $app _key = ' 314**************60191 ';        The application to be sent (AppKey) can only be filled in one.    Private $master _secret = ' ab16a***********835c8862 ';      Master Password Private $url = "Https://api.jpush.cn/v3/push"; The address of the push//if the corresponding value is passed in as instantiated, the public function __construct ($app _key=null, $master _secret=null, $url =nul L) {if ($app _key) $this->app_key = $app _key; if ($master _secret) $this->master_secret = $master _secret; if ($url) $this->url = $url; }/////For the class//of the Aurora push: Http://docs.jpush.cn/display/dev/Push-API-v3/** assembly Required Parameters $receive = ' all ',//all $receive = Array (' tag ' =& Gt;array (' 2401 ', ' 2588 ', ' 9527 ');//Tag $receive = Array (' Alias ' =>array (' 93d78b73611d886a74*****88497f501 '));//Alias $content = ' This is a test of push data .... Test.... Hello world ... '; $m _type = ' http '; $m _txt = ' http://www.iqujing.com/'; $m _time = ' 600 '; Offline retention time **///Call push method Public function Send_pub ($receive, $content, $m _type, $m _txt, $m _time{$m _type = ' http ';//The type of the push additional field $m _txt = ' http://www.groex.cn/';//the content of the type that pushes the additional field (optional) may be a URL, possibly a paragraph of text. $m _time = ' 86400 ';//offline retention time $message = "";//store push status $result = $this->push ($receive, $content, $m _type, $m _txt, $m _time); if ($result{$res _arr = Json_decode ($result, True); if (isset ($res _arr[' error ')) {//If error is returned, proof of failure echo $res _arr[' ERROR ' [' message '];//error message $error _code= $res _arr[' Error ' [' Code ']; Error code switch ($error _code) {case: $message = ' sent successfully! ‘; Break; Case: $message = ' failure (System internal error) '; Break; Case 1001: $message = ' failed (HTTP Post method only supported, Get method not supported) '; Break; Case 1002: $message = ' failed (missing required parameter) '; Break; Case 1003: $message = ' failed (parameter value is not valid) '; Break; Case 1004: $message = ' failed (verification failed) '; Break; Case 1005: $message = ' failed (message body too Large) '; Break; Case 1008: $message = ' failed (appkey parameter illegal) '; Break; Case 1020: $message = ' failed (HTTPS request only) '; Break; Case 1030: $message = ' failed (internal service timeout) '; Break; Default: $message = ' failed (return to other status, not currently known, please contact the developer!) )‘; Break; }}else{$message = "sent successfully! "; }}else{//interface call failed or no response $message = ' interface call failed or not responding '; } echo "<script>alert (' push message: {$message} ') </script>"; }/* $receiver the recipient's information all string for all users below the product. Push messages for all users under App_key tag (20) array tag Group (set): Tag=>array (' Kunming ', ' Beijing ', ' Qujing ', ' Shanghai '); Tag_and (20) Array Label Group (intersection): Tag_and=>array (' Guangzhou ', ' Female '); Alias (+) array aliases (SET): Alias=>array (' 93d78b73611d886a74*****88497f501 ', ' 606d05090896228f66ae10d1*****310 '); REGISTRATION_ID (1000) Registration ID device identity (SET): Registration_id=>array (' 20effc071de0b45c1a********** 2824746e1ff2001bd80308a467d800bed39e '); *///$content push content. $m _type type of push additional field (optional) http,tips,chat ....//$m _txt The type of the additional field to push (not fill in) may be a URL, possibly a text. $m _time the number of seconds to save offline the default is one day (can not pass) the unit is the second public function push ($receiver = "All", $content = "', $m _type=", $m _txt= ", $m _time= ' 86400 '{$base 64=base64_encode ("$this->app_key: $this->master_secret")); $header =array ("Authorization:basic $base", "Content-type:application/json"); $data = array(); $data [' platform '] = ' all '; Target user terminal phone platform type android,ios,winphone $data [' audience '] = $receiver; Target user $data [' notification '] = array(//Unified mode-Standard mode "alert" = + $content,//Android Custom "Android" =>array("alert" + $content, "title" = "" "," builder_id "=>1," extras "=>array (" type "= + $m _type," txt "= $m _txt)),//ios Custom "ios" =>array("alert") and $content, "badge" and "1", "sound" = "default", "Extras" =>array ("type" and "=" = $m _type, "txt" + = $m _txt) ) ); Apple Custom---Easy to debug for pop-up values $data [' message '] = array("msg_content" = $content, "Extras" =>array ("type" and "=" = $m _type, "txt" = $m _txt) ); Additional Options $data [' options '] = array("Sendno" =>time (), "time_to_live" = $m _time,//The number of seconds to save offline the default is one day "apns_production" =>false,//boolean type designation APNs Notification delivery Environment: 0 development environment, 1 production environment. or pass false and True); $param = Json_encode ($data); $res = $this->push_curl ($param, $header); if ($res) {//Get return value-----------$res else{//did not get return value-return failed returns false;}}//Push the Curl method public Function Push_curl ($param = "", $header = "") {if (Empty ($pa RAM) {return false;} $postUrl = $thisurl; $curlPost = $param; $ch = Curl_init ();//Initialize Curl curl_setopt ($ch, Curlopt_url, $POSTURL); Crawl specified Web page curl_setopt ($ch, Curlopt_header, 0); Set Header curl_setopt ($ch, Curlopt_returntransfer, 1); Requires the result to be a string and output to the screen curl_setopt ($ch, Curlopt_post, 1); Post Submission Method curl_setopt ($ch, Curlopt_postfields, $curlPost); curl_setopt ($ch, Curlopt_httpheader, $header);//Increase HTTP Header (header) field curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE); Terminates validation from the server curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE), $data = Curl_exec ($ch);//Run Curl Curl_close ($ch  ); return $data;}}? >           

Aurora Push Jpush (v3) server PHP version integration (V3 version only calls the Push API)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.