下面我們來看一篇關(guān)于thinkphp 寫APP接口集成極光推送,對于app的數(shù)據(jù)都得通過接口來實現(xiàn)了,當然也有內(nèi)置數(shù)據(jù)庫的不過這種非常少了。
最近用Thinkphp寫了個App接口用到第三方推送功能,本文用的第三方推送工具是極光推送,其他的推送不一一介紹。
第一步:下載PHPSDK 并到官網(wǎng)上注冊應(yīng)用將app_key 、master_secret 放到文件中
下載地址:https://www./common/downloads/resource/1460966988359
第二步:將解壓后的文件 src->JPush中的文件及文件夾復(fù)制到ORG->Push中(文件夾需要自己創(chuàng)建)或者放到vender中
第三步:在公共控制器CommonAction中創(chuàng)建push方法
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("這是測試的推送")
->send();
//echo 'Result=' . json_encode($result) . $br;
}
這樣就可以完成基本的推送
其他功能本項目沒有涉及到就沒有書寫了解更多:
https://github.com/jpush/jpush-api-php-client/blob/master/doc/api.md#device-api
======華麗的分格線======
另一種方式:
<?php
//jpush.php 這是推送方法 用到curl發(fā)送請求
class jpush {
private $_masterSecret = '';
private $_appkeys = '';
/**
* 構(gòu)造函數(shù)
* @param string $username
* @param string $password
* @param string $appkeys
*/
function __construct($masterSecret = '',$appkeys = '') {
$this->_masterSecret = $masterSecret;
$this->_appkeys = $appkeys;
}
/**
* 模擬post進行url請求
* @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();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網(wǎng)頁
curl_setopt($ch, CURLOPT_HEADER, 0);//設(shè)置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//運行curl
curl_close($ch);
return $data;
}
/**
* 發(fā)送
* @param int $sendno 發(fā)送編號。由開發(fā)者自己維護,標識一次發(fā)送請求
* @param int $receiver_type 接收者類型。1、指定的 IMEI。此時必須指定 appKeys。2、指定的 tag。3、指定的 alias。4、 對指定 appkey 的所有用戶推送消息。
* @param string $receiver_value 發(fā)送范圍值,與 receiver_type相對應(yīng)。 1、IMEI只支持一個 2、tag 支持多個,使用 "," 間隔。 3、alias 支持多個,使用 "," 間隔。 4、不需要填
* @param int $msg_type 發(fā)送消息的類型:1、通知 2、自定義消息
* @param string $msg_content 發(fā)送消息的內(nèi)容。 與 msg_type 相對應(yīng)的值
* @param string $platform 目標用戶終端手機的平臺類型,如: android, ios 多個請使用逗號分隔
*/
function send($sendno = 0,$receiver_type = 1, $receiver_value = '',
$msg_type = 1, $msg_content = '', $platform = 'android,ios') {
$url = 'http://api.: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;
}
}
?>
調(diào)用方式:
<?php
include('jpush.php');
$n_title = '驛泊';
$n_content = '驛泊人生';
$arr=array('fromer'=>'發(fā)送者','fromer_name'=>'發(fā)送者名字','fromer_icon'=>'發(fā)送者頭像','image'=>'發(fā)送圖片鏈接','sound'=>'發(fā)送音樂鏈接');//自定義參數(shù)
$appkeys='先上傳app應(yīng)用項目,自動生成的key';
$masterSecret='appkey下邊就同樣生成mastersecret的秘鑰';
$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();
?>
|