<?php/* push test */namespace service\controller;use think\controller;class Pushcontroller extends Controller {private $_ Config = array ();p ublic function __construct () {parent::__construct (); $this->_config = $this->_makeconfig ();} /* Android send */public function ptoandroid ($device _tokens, $ticker, $title, $text) {if (count ($device _tokens) >) {die (' Devices over 500 ');} Stitching signature $post_data = $this->_android ($device _tokens, $ticker, $title, $text); $sign = $this->_makesign ($post _data, 1); $url = $this->_config[' url ']. '? sign= '. $sign;//Send Request $res = $this->_curl ($url, $post _data);//Determine if ($res [' ret ']! = ' SUCCESS ') {//Send failed, die ($res [' Data ']-> Error_code);}} Public Function TestA () {$device _tokens = array (' Akfwclptmtjbeof17rsnsaypux4ayvzi6dru3o1avuse '); $this Ptoandroid ($device _tokens, ' text of the Notification bar ', ' Test title ', ' Test content '); /*ios Send */public function Ptoios ($device _tokens, $text) {if (count ($device _tokens) > x) {die (' device more than 500 ');} Splicing Post Data $post_data = $this->_ios ($device _tokens, $text);//stitching signature $sign = $this->_makESign ($post _data,2); $url = $this->_config[' url ']. '? sign= '. $sign;//Send Request $res = $this->_curl ($url, $post _data);//Determine if ($res [' ret ']! = ' SUCCESS ') {//Send failed, die ($res [' Data ']-> Error_code);}} Public Function Testios () {$device _tokens = array (' DD61807869EFB4664491CD36ECA8843A111A96D39E9772EA7940F0E83CE217C6 $this->ptoios ($device _tokens, ' Hello ');} Generate config file private function _makeconfig () {return Array (' method ' = ' + ' POST ', ' url ' = ' http://msg.umeng.com/api/send ') , ' ios_app_key ' = ' 55541bb767e58e2094000492 ', ' ios_app_master_secret ' = ' wbo2vnu5wmdvzfwzc2axdiiojj5ujkwt ', ' And_app_key ' = ' 5551b48767e58ec961001d17 ', ' and_app_master_secret ' = ' wjnbje5ju9rf89opo2p30phywwjqsavy ',);} /* Generate signature @param post_body request body Type 1, Android, 2 Ios@return str*/private function _makesign ($post _body, $type) {if ($type = = 1) return Strtolower (MD5 ($this->_config[' method '). $this->_config[' url ']. $post _body. $this->_config[' And_app_ Master_secret ']), else return Strtolower (MD5 ($this->_config[' method‘] . $this->_config[' url ']. $post _body. $this->_config[' Ios_app_master_secret '));} /*android post_body set @param ticker str cue bar text title str notification title text str text description device_tokens array device number @return */private function _ Android ($device _tokens, $ticker, $title, $text) {$temp _arr = array (' appkey ' = + $this->_config[' And_app_key '], ' Timestamp ' = + time (), ' type ' = ' listcast ', ' device_tokens ' = Trim (implode (', ', $device _tokens), ', '), ' payload ' = = Array (' display_type ' = ' notification ',//Notify, AU takes over processing ' body ' = = Array (' ticker ' + $ticker, ' title ' = = Title, ' text ' = $text, ' after_open ' = ' go_custom ', ' custom ' = ' do things ',//Click to do after the notification, ' description ' and ' = ' ,//description ' production_mode ' = ' false ',//test, online is true); return Json_encode ($temp _arr);} /*ios post_body message body Settings @param ticker str cue bar text title str notification title text str text description device_tokens array device number @return */private function _i OS ($device _tokens, $text) {$temp _arr =array (' appkey ' = $this->_config[' ios_app_key '], ' timestamp ' = time (), ' type ' = ' listcast ', ' device_tokens ' = Trim (implode (', ', ', $device _tokens), ', '), ' payload ' = = Array (' APS ' = > Array (' alert ' = $text, ' after_open ' = ' go_custom ', ' custom ' = ' do things ',//Click to do after the notification), ' description ' =& Gt ' Listcast ',//description ' production_mode ' = ' false ',//test, online is true); return Json_encode ($temp _arr);} /* Splicing Curl Request */private function _curl ($url, $post) {$ch = Curl_init ($url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_binarytransfer, 1); curl_setopt ($ch, curlopt_post,1); curl_setopt ($ch, curlopt_connecttimeout, 60); curl_setopt ($ch, curlopt_timeout, 60); curl_setopt ($ch, Curlopt_postfields, $post); $result = curl_exec ($ch); $httpCode = Curl_getinfo ($ch, Curlinfo_http_code); $curlErrNo = Curl_errno ($ch); $CURLERR = Curl_error ($ch); Curl_close ($ch); if ($httpCode = = "0") die ("Curl Error Number:".) $curlErrNo. ", Curl Error Details:". $curlERr. "\ r \ n"); else if ($httpCode! = "$") Die ("Http Code:".) $httpCode. "Details:". $result. "\ r \ n"); else return (array) Json_decode ($result);}}
Friend League push, PHP simple code