Recently used thinkphp to write a app interface to use Third-party push function, this article uses the third party push tool is the aurora push, other push send not one by one introduction.
The first step: Download PHPSDK and register the application on the official website to put App_key, master_secret into the file
Download Address: https://www.jpush.cn/common/downloads/resource/1460966988359
Step two: Copy the files and folders in the extracted file Src->jpush to Org->push (folders need to be created by themselves) or put them in vender
Step three: Create a push method in the public controller commonaction
Private $app _key=****;
Private $master _secret=****;
Public Function push ()
{
Import ("ORG.") Push.push ");
$client = new Jpush ($this->app_key, $this->master_secret);
$result = $client->push ()
->setplatform (' All ')
->addallaudience ()
->setnotificationalert ("This is a test push")
->send ();
Echo ' result= '. Json_encode ($result). $BR;
}
This will allow you to complete the basic push
Other features this project does not involve writing about no more:
Https://github.com/jpush/jpush-api-php-client/blob/master/doc/api.md#device-api
The magnificent line of the penny = = =
Another way:
<?php
Jpush.php This is a push method to send a request using the Curl
Class Jpush {
Private $_mastersecret = ';
Private $_appkeys = ';
/**
* Constructor
* @param string $username
* @param string $password
* @param string $appkeys
*/
function __construct ($masterSecret = ', $appkeys = ') {
$this->_mastersecret = $masterSecret;
$this->_appkeys = $appkeys;
}
/**
* Simulate post for URL request
* @param string $url
* @param string $param
*/
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);//crawl the specified page
curl_setopt ($ch, Curlopt_header, 0);/Set HEADER
curl_setopt ($ch, Curlopt_returntransfer, 1);/request result is string and output to 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 send number. Maintained by the developer, identifying a single send request
* @param int $receiver _type receiver type. 1, the specified IMEI. Appkeys must be specified at this time. 2, the designated tag. 3, the specified alias. 4. Push messages to all users of the specified appkey.
* @param string $receiver _value to send a range value corresponding to the Receiver_type. 1, IMEI only support a 2, tag support multiple, using the "," interval. 3. Alias supports multiple, use "," interval. 4, do not need to fill
* @param int $msg _type The type of message sent: 1, notification 2, custom message
* @param string $msg _content The contents of the message. The value corresponding to the Msg_type
* @param string $platform The platform type of the target user's terminal phone, such as: Android, iOS multiple please use comma separated
*/
function Send ($sendno = 0, $receiver _type = 1, $receiver _value = ', $msg _type = 1, $msg _content = ', $platform = ' Android , 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;
}
}
?>
Call Mode:
<?php
Include (' jpush.php ');
$n _title = ' Yi Bo ';
$n _content = ' life in the park ';
$arr =array (' fromer ' => ' sender ', ' fromer_name ' => ' sender name ', ' fromer_icon ' => ' sender avatar ', ' image ' => ' send picture link ', ' sound ' => ' send music link ');//Custom parameters
$appkeys = ' Upload first App application project, automatically generated key ';
$masterSecret = ' Appkey the same mastersecret secret key ';
$sendno = 4;
$receiver _value = ';
$platform = ' 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 ();
?>