PHP: use Aurora to push messages

Source: Internet
Author: User
Tags sendmsg
First, we create a class: (The following is the class code) so that our call class is complete. Next, we can call this class so that we can push it, I also learn from other people's code, hoping to help some friends in need. First, create a class: (The following is the code of the class)
Class jpush {private $ _ masterSecret = ''; private $ _ appkeys =''; function construct ($ masterSecret = '', $ appkeys = '') {$ this-> _ masterSecret = $ masterSecret; $ this-> _ appkeys = $ appkeys ;} /*** simulate post url request * @ param string $ url * @ param string $ param */function request_post ($ url = '', $ param = '') {if (empty ($ url) | empty ($ param) {return false;} $ base64_auth_string = base64_encode ("$ this-> _ Ppkeys: $ this-> _ masterSecret "); $ header = array (" Authorization: Basic $ base64_auth_string "); $ postUrl = $ url; $ curlPost = $ param; $ ch = curl_init (); // initialize curl curl_setopt ($ ch, CURLOPT_URL, $ postUrl); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HEADER, 0 ); // Set the header curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result must be a string and be output to the screen curl_setopt ($ ch, CURLOPT_POST, 1 ); // post submission method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPo St); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); $ data = curl_exec ($ ch); // run curl curl_close ($ ch); return $ data ;} /*** send ** @ param int $ sendno. Maintained by the developer, it identifies the type of the receiver sending a request * @ param int $ receiver_type. 1. the specified IMEI. AppKeys must be specified. 2. the specified tag. 3. the specified alias. 4. push messages to all users with the specified appkey. * @ Param string $ receiver_value refers to the sending range value, which corresponds to receiver_type. 1. Only one IMEI is supported. 2. multiple tags are supported. use the "," interval. 3. alias supports multiple, with "," interval. 4. do not enter * @ param int $ msg_type to send the message type: 1. notification 2. Custom message * @ param string $ msg_content to send the message content. Value corresponding to msg_type * @ param string $ platform type of the target user's terminal mobile phone, such as android, for ios, use commas to separate multiple functions */function send ($ sendno = 0, $ receiver_type = 1, $ receiver_value = '', $ msg_type = 1, $ msg_content = '', $ platform = 'iOS ') {$ url =' http://api.jpush.cn:8800/sendmsg/v2/sendmsg '; $ Param = ''; $ param. = '& sendno = '. $ sendno; $ appkeys = $ this-> _ appkeys; $ param. = '& app_key = '. $ appkeys; $ param. = '& receiver_type = '. $ receiver_type; $ param. = '& receiver_value = '. $ receiver_value; $ masterSecret = $ this-> _ masterSecret; $ verification_code = md5 ($ sendno. $ receiver_type. $ receiver_value. $ masterSecret); $ param. = '& verification_code = '. $ verification_code; $ param. = '& msg_type = '. $ msg_type; $ param. = '& msg_content = '. $ msg_content; $ param. = '& platform = '. $ platform; $ res = $ this-> request_post ($ url, $ param); if ($ res = false) {return false ;} $ res_arr = json_decode ($ res, true); return $ res_arr ;}}
In this way, our call class is complete, and the next step is how to call this class
$ N_title = ''// sent title; $ n_content ='' // sent content; $ arr = array ('fromer '=> 'sender ', 'fromer _ name' => 'sender name', 'fromer _ icon '=> 'sender profile picture', 'image' => 'Send image link ', 'sound' => 'Send music link', 'pagetype '=> 0); // custom parameter $ appkeys = 'fill in the appkey' of Your Aurora push '; $ masterSecret = 'Enter your Aurora push masterSecret '; $ sendno = 4; // send ID. Maintained by the developer, the $ receiver_value = '4' of a sending request is identified; // The sending range value, which corresponds to the receiver_type $ platform = 'iOS '; // The type of the target terminal, if it is a full platform, you can directly write it as "all". if it is an android app and an Apple app, separate them by commas (,) ('Android, ios ') $ msg_content = json_encode (array ('n' _ builder_id '=> 0, 'n' _ title' => $ n_title, 'n' _ content' => $ n_content, 'n' _ extras '=> $ arr); $ obj = new jpush ($ masterSecret, $ appkeys); $ res = $ obj-> send ($ sendno, 4, $ receiver_value, 1, $ msg_content, $ platform); print_r ($ res); exit ();

The above is PHP: use Aurora to push message details. For more information, see other related articles in the first PHP community!

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.