Php push example (third-party Aurora push)-PHP source code

Source: Internet
Author: User
Tags php server sendmsg
Php is a scripting language, so it cannot be used well in real-time chat. Especially when the server pushes information to the mobile phone, if there is a little more information, it will cause pressure on the php server, however, we can also use third-party Aurora push php as a scripting language, so it cannot be used in real-time chat, especially when php is used to push information to mobile phones, if there is a little more information, this will put pressure on php servers, but we can also use third-party Aurora push.

Script ec (2); script

We only need to apply on Aurora website, and then record the appkey and write it in the code. I tested it in thinkphp.

The Code is as follows:

Class ApipostAction extends Action {
/**
* Simulate post for url requests
* @ Param string $ url
* @ Param string $ param
*/
Private $ _ appkeys = '**********************';
Private $ _ masterSecret = '**********************';

Function request_post ($ url = ", $ param = "){
If (empty ($ url) | empty ($ param )){
Return false;
}
$ 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 is a string and output to the screen.
Curl_setopt ($ ch, CURLOPT_POST, 1); // post submission method
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );
$ Data = curl_exec ($ ch); // run curl
Curl_close ($ ch );
Return $ data;
}
/**
* Send
* @ Param int $ sendno sending number. Maintained by the developer, the sender ID is a request
* @ Param int $ receiver_type: 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. IMEI only supports one. 2. Multiple tags are supported. Use the "," interval. 3. alias supports multiple IDs. Use the "," interval. 4. Not Required
* @ Param int $ msg_type: 1. Notification 2. Custom message
* @ Param string $ msg_content refers to the content of the message sent. Value corresponding to msg_type
* @ Param string $ platform type of the target user's terminal mobile phone, such as android and ios, separated by commas
*/
Function send ($ sendno = 0, $ receiver_type = 1, $ receiver_value = ", $ msg_type = 1, $ msg_content =", $ platform = 'android '){
$ 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 );

$ Res_arr = json_decode ($ res, true );
If (intval ($ res_arr ['errcode'])! = 0 ){
Return false;
} Else {
Return true;
}
/* If ($ res = false ){
Return false;
}
$ Res_arr = json_decode ($ res, true );
$ Res_arr ['errmsg '] = "no error message ";
Switch (intval ($ res_arr ['errcode']) {
Case 0: $ res_arr ['errmsg '] = 'sent successfully'; break;
Case 10: $ res_arr ['errmsg '] = 'internal system error'; break;
Case 1001: $ res_arr ['errmsg '] = 'only the HTTP Post method is supported, and the Get method is not'; break;
Case 1002: $ res_arr ['errmsg '] =' a required parameter is missing '; break;
Case 1003: $ res_arr ['errmsg '] = 'invalid parameter value'; break;
Case 1004: $ res_arr ['errmsg '] = 'verification failed'; break;
Case 1005: $ res_arr ['errmsg '] = 'message body too large'; break;
Case 1007: $ res_arr ['errmsg '] = 'invalid Er _ value parameter'; break;
Case 1008: $ res_arr ['errmsg '] = 'invalid appkey parameter'; break;
Case 1010: $ res_arr ['errmsg '] = 'msg _ content invalid'; break;
Case 1011: $ res_arr ['errmsg '] = 'no push target meets the condition'; break;
Case 1012: $ res_arr ['errmsg '] = 'ios does not support pushing custom messages. Only Android supports pushing custom messages '; break;
Default: break;
}
$ Msg_content = json_decode ($ msg_content, true );
If (intval ($ res_arr ['errcode']) = 0 ){
$ Str ="

  • ". $ Res_arr ['sendno']." Send ". $ res_arr ['errmsg ']."!
  • ";
    } Else {
    $ Str ="
  • ". $ Res_arr ['sendno']." failed to send: ". $ res_arr ['errmsg ']."
  • ";
    }
    Print_r ($ str); die ();*/
    }
    }
    ?>

    Call
    $ Receiver_value = 1; // The receiver id is the same as n_builder_id
    $ Platform = 'android, ios '; // received on the above mobile phone
    $ Msg_content = json_encode (array ('n' _ builder_id '=> '1', 'n' _ title' => 'title', 'n' _ content' => 'content ', 'n' _ extras '=> array ('fromer' => 'sender', 'fromer _ name' => 'sender name ', 'fromer _ icon '=> 'sender profile', 'image' => 'send image link', 'sound' => 'send music link ')));
    $ This-> send ($ sendno, 3, $ receiver_value, 1, $ msg_content, $ platform );
    ?>

    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.