了解最新公司動(dòng)態(tài)及行業(yè)資訊
文章目錄
很多人都有自己的服務(wù)器。尤其是對(duì)大學(xué)生來說服務(wù)器運(yùn)維服務(wù)器運(yùn)維,很多服務(wù)器公司對(duì)中學(xué)生都是有利的。比如騰訊云每個(gè)月只要1塊錢:所以作為程序員,我們總是想自己掌控一切。在手,如何使用我們的開發(fā)者服務(wù)器管理陌陌公眾號(hào)?這就是本文所涵蓋的內(nèi)容。
本文介紹了以下內(nèi)容:
1 如何將陌陌公眾號(hào)授權(quán)給開發(fā)者服務(wù)器 2 如何使用代碼將開發(fā)者服務(wù)器與陌陌服務(wù)器關(guān)聯(lián)
/**
* 微信公眾號(hào)開發(fā)-入門
*
* api
*/
define("TOKEN",'we--xxxx'); //這里和你微信公眾號(hào)開放平臺(tái)上的tonken填寫一樣的即可
$weixinApi=new WeixinApi();
if(isset($_GET["echostr"])){
$weixinApi->valid();
}else{
$weixinApi->responseMsg();
}
class WeixinApi{
//驗(yàn)證接口
public function valid(){
$echoStr = $_GET["echostr"];//從微信用戶端獲取一個(gè)隨機(jī)字符賦予變量echostr
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
//檢查簽名
private function checkSignature(){
//1 接受微信服務(wù)器get請求發(fā)送過來的4個(gè)參數(shù)
$signature = $_GET["signature"];//從用戶端獲取簽名賦予變量signature
$timestamp = $_GET["timestamp"];//從用戶端獲取時(shí)間戳賦予變量timestamp
$nonce = $_GET["nonce"]; //從用戶端獲取隨機(jī)數(shù)賦予變量nonce
//2 加密和校驗(yàn)請求
//2.1 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序
$tmpArr = array(TOKEN, $timestamp, $nonce);//簡歷數(shù)組變量tmpArr
sort($tmpArr, SORT_STRING);//新建排序
//2.2 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密

$tmpStr = implode($tmpArr);//數(shù)組轉(zhuǎn)字符串
$tmpStr = sha1($tmpStr);//shal加密
//2.3 開發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該請求來源于微信
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
//回復(fù)消息
public function responseMsg(){
//3 以下代碼接受消息
//3.1 接受微信服務(wù)器發(fā)送過來的原生的POST的數(shù)據(jù)包
// $postData = $GLOBALS["HTTP_RAW_POST_DATA"];
$postData = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] :file_get_contents("php://input");
//3.2 處理數(shù)據(jù)包
$xmlObj = simplexml_load_string($postData, "SimpleXMLElement", LIBXML_NOCDATA);
$msgType = $xmlObj->MsgType;
//4 根據(jù)消息類型進(jìn)行業(yè)務(wù)處理
switch ($msgType) {
//接受事件消息
case 'event':
$this->disposeEvent($xmlObj);
break;
//接受文本消息
case 'text':
$this->disposeText($xmlObj);
break;
//接受圖片消息
case 'image':
$this->disposeImage($xmlObj);
break;
}
}
//處理接收的事件消息
private function disposeEvent($xmlObj){
switch ($xmlObj->Event){
case 'subscribe'://訂閱事件
$this->sendText('歡迎您的訂閱');
break;
case 'unsubscribe'://取消訂閱事件
$this->sendText('good-bye');//該消息用戶其實(shí)是看不到的,取消訂閱事件一般用來清除數(shù)據(jù)庫記錄
break;
}
}
//處理接收的文本消息
private function disposeText($xmlObj){
$text=trim($xmlObj->Content);
//包含關(guān)鍵字都不做處理
if (!(
strstr($text,'違規(guī)') //這里對(duì)違規(guī)的關(guān)鍵字做排除,不予理睬
)){
switch ($text){
case '你好':
$this->sendText($xmlObj,'Hi 我是開發(fā)者服務(wù)器');
break;
case 'new':
$newsArr=array(
array(
"title"=>"看到這條消息,你可以買彩票了",
"description"=>"本公眾號(hào)有許多小彩蛋,歡迎您的探索。",
"picUrl"=>"http://img.mp.itc.cn/upload/20170610/03d69e8df0524b8cb59fd16dc2fec989.jpg",
"url"=>"http://www.baidu.com"
)
);
$this->sendNews($xmlObj,$newsArr);
break;
default:
$this->tuling123($xmlObj,trim($xmlObj->Content)); //圖靈機(jī)器人
}
}
}
//處理接收的圖片消息
private function disposeImage($xmlObj){ //一般情況下,不會(huì)去處理用戶發(fā)送的圖片
$this->sendImage($xmlObj,$xmlObj->PicUrl,$xmlObj->MediaId);
}
//發(fā)送文本的方法
private function sendText($xmlObj,$content){
$replyTextMsg="
%s
";
echo sprintf($replyTextMsg,$xmlObj->FromUserName,$xmlObj->ToUserName,time(),$content);
}
//發(fā)送圖片的方法
private function sendImage($xmlObj,$mediaId){
$replyImageMsg="
%s
";
echo sprintf($replyImageMsg,$xmlObj->FromUserName,$xmlObj->ToUserName,time(),$mediaId);
}
//發(fā)送圖文的方法
private function sendNews($xmlObj,$newsArr){
$newsTplHead = "
%s
%s
" ;
$newsTplBody = "-
";
$newsTplFoot = "
%s
";
$replyNewsMsg = sprintf($newsTplHead, $xmlObj->FromUserName, $xmlObj->ToUserName, time(),count($newsArr));
foreach($newsArr as $key => $value){
$replyNewsMsg .= sprintf($newsTplBody, $value['title'], $value['description'], $value['picUrl'], $value['url']);
}
$replyNewsMsg .= sprintf($newsTplFoot, 0);
echo $replyNewsMsg;
}
public function tuling123($xmlObj,$message){//這是是使用圖靈機(jī)器人
$tuTonken='2d8aaa17141c443----xxx---fsa'; //請去圖靈網(wǎng)http://www.tuling123.com/自己申請一個(gè)tonken
$tuUrl='http://www.tuling123.com/openapi/api?key='.$tuTonken.'&info='.$message.'&userid='.$xmlObj->FromUserName;
$tuData='{
"key": "'.$tuTonken.'",
"info": "'.$message.'",
"userid": "'.$xmlObj->FromUserName.'"
}';
$results = $this->htts_request($tuUrl,$tuData);
// print_r($results);
if ($results['code']==100000){
$text=$results['text'];
$this->sendText($xmlObj,$text);
}else{
$this->sendText($xmlObj,'有問題,請輸入“幫助”');
}
}
//https請求(get和post)
private function htts_request($url,$data=array()){

//1 初始化curl
$ch=curl_init();
//2 設(shè)置傳輸選項(xiàng)
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//把頁面以文件流的形式返回
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, true); //設(shè)置為 POST 請求
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //設(shè)置POST的請求數(shù)據(jù)
}
//3 執(zhí)行curl請求
$outopt=curl_exec($ch);
$outoptArr=json_decode($outopt,true);
//4 關(guān)閉curl
curl_close($ch);
return $outoptArr;
}
}
?>
//網(wǎng)頁授權(quán)-base型
public function snsapi_base($redirect_uri){
//以下是測試賬號(hào)
$appid="wxb4----xxx";//請?jiān)诘谝徽碌?小節(jié)的圖片中看
$appsecret="21da56-----xxx";//請?jiān)诘谝徽碌?小節(jié)的圖片中看
//準(zhǔn)備scope
$snsapi_base_url="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=SCOPE&state=123#wechat_redirect";
$code=$_GET['code'];
//獲取code
if (!isset($code)){
header("Location:{$snsapi_base_url}");
}
//獲取access_token
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
return $this->htts_request($url);
}
24小時(shí)免費(fèi)咨詢
請輸入您的聯(lián)系電話,座機(jī)請加區(qū)號(hào)