亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 微信小程式 微信開發(fā) PHP微信開發(fā)之微信訊息自動回覆遇到的問題

PHP微信開發(fā)之微信訊息自動回覆遇到的問題

Mar 21, 2017 pm 04:10 PM

這篇文章是小編給大家介紹的微信訊息自動回覆下所遇到的坑的相關(guān)內(nèi)容,在日常專案開發(fā)中經(jīng)常遇到,非常具有參考借鑒價值,感興趣的小伙伴一起學(xué)習(xí)吧

微信回覆原理:

當(dāng)普通微信用戶向公眾帳號發(fā)送訊息時,微信伺服器首先收到用戶發(fā)送的訊息;

然後將使用者資訊和訊息打包成XML格式的資料包,再將這個XML資料包透過POST方法提交到開發(fā)者設(shè)定的URL上。

疑問一:為何使用$GLOBALS["HTTP_RAW_POST_DATA"]保存POST過來的數(shù)據(jù),而非$_POST數(shù)組

回答:

POST只能保存標(biāo)準(zhǔn)的資料型別,對於XML、SOAP或Application/Octet-steam之類的內(nèi)容則無法解析。

而$GLOBALS["HTTP_RAW_POST_DATA"]和$_POST是一樣的,如果POST過來的資料PHP能夠識別,則可以用$GLOBALS["HTTP_RAW_POST_DATA"]來接收。

疑問二:simplexml_load_file()各參數(shù)和回傳值是什麼?

回答:

參數(shù)意義

string:需要處理的XML字串

class:用來指定新物件,通常設(shè)定為"SimpleXMLElement",產(chǎn)生一個簡單XML元素的類別。

options:指定附加的Libxml參數(shù),通常設(shè)定為常數(shù)LIBXML_NOCDATA,表示把CDATA設(shè)定為文字節(jié)點。

ns:一般省略

is_prefix:一般省略

函數(shù)執(zhí)行完成後傳回SimpleXMLElement類別的一個物件。

功能:公眾號只接受文字訊息,並且做出對應(yīng)的文字回覆。

<span style="font-family:Courier New;font-size:14px;"><?php 
define("TOKEN","weixin"); 
$weixinObj = new Wechat(); 
$weixinObj->valid();?
class?Wechat{?
public?function?valid(){?
$echoStr?=?$_GET['echostr'];?
//如果是第一次接入?
if($this->checkSignature()?&&?$echoStr?){?
echo?$echoStr;?
exit;?
}else{?
$this->responseMsg();?
}?
}?
//校驗方法?
private?function?checkSignature(){?
$signature?=?$_GET['signature'];?
$timestamp?=?$_GET['timestamp'];?
$nonce?=?$_GET['nonce'];?
$token?=?TOKEN;?
$tmpArr?=?array($token,?$timestamp,?$nonce);?
sort($tmpArr);?
$tmpStr?=?implode($tmpArr);?
$tmpStr?=?sha1($tmpStr);?
if($tmpStr?==?$signature){?
return?true;?
}else{?
return?false;?
}?
}?
/*?普通文本消息?
<xml>?
<ToUserName><![CDATA[toUser]]></ToUserName>?
<FromUserName><![CDATA[fromUser]]></FromUserName>?
<CreateTime>1348831860</CreateTime>?
<MsgType><![CDATA[text]]></MsgType>?
<Content><![CDATA[this is a test]]></Content>?
</xml>?
*/?
public?function?responseMsg(){?
//獲取微信服務(wù)器POST請求中的數(shù)據(jù)?
$postStr?=?$GLOBALS["HTTP_RAW_POST_DATA"];?
if(?!empty($postStr)?){?
$postObj?=?simplexml_load_string($postStr,?'SimpleXMLElement',?LIBXML_NOCDATA);?
$fromUser?=?$postObj->FromUserName;?
$toUser?=?$postObj->ToUserName;?
$keyword?=?trim($postObj->Content);?
$time?=?time();?
$template?=?"<xml>?
<ToUserName><![CDATA[%s]]></ToUserName>?
<FromUserName><![CDATA[%s]]></FromUserName>?
<CreateTime>%s</CreateTime>?
<MsgType><![CDATA[%s]]></MsgType>?
<Content><![CDATA[%s]]></Content>?
</xml>";?
if(?strtolower($postObj->MsgType)!='text'?){?
$msgType?=?"text";?
$content?=?"我只接受文本消息";?
}else{?
$msgType?=?"text";?
if(?!empty($keyword)?){?
$content?=?"您發(fā)送的消息是:".$postObj->Content;?
}else{?
$content?=?"請輸入關(guān)鍵字";//消息為空?
}?
}?
$info?=?sprintf($template,?$fromUser,?$toUser,?$time,?$msgType,?$content);?
echo?$info;?
}else{?
echo?"";?
exit;?
}?
}?
}</span>

功能:公眾號只接受圖片訊息,並且做出對應(yīng)的文字回應(yīng)。

<span style="font-family:Courier New;font-size:14px;"><?php 
define("TOKEN","weixin"); 
$weixinObj = new Wechat(); 
$weixinObj->valid();?
class?Wechat{?
public?function?valid(){?
$echoStr?=?$_GET['echostr'];?
//如果是第一次接入?
if($this->checkSignature()?&&?$echoStr?){?
echo?$echoStr;?
exit;?
}else{?
$this->responseMsg();?
}?
}?
//校驗方法?
private?function?checkSignature(){?
$signature?=?$_GET['signature'];?
$timestamp?=?$_GET['timestamp'];?
$nonce?=?$_GET['nonce'];?
$token?=?TOKEN;?
$tmpArr?=?array($token,?$timestamp,?$nonce);?
sort($tmpArr);?
$tmpStr?=?implode($tmpArr);?
$tmpStr?=?sha1($tmpStr);?
if($tmpStr?==?$signature){?
return?true;?
}else{?
return?false;?
}?
}?
/*?接收圖片消息格式?
<xml>?
<ToUserName><![CDATA[toUser]]></ToUserName>?
<FromUserName><![CDATA[fromUser]]></FromUserName>?
<CreateTime>1348831860</CreateTime>?
<MsgType><![CDATA[image]]></MsgType>?
<PicUrl><![CDATA[this is a url]]></PicUrl>?
<MediaId><![CDATA[media_id]]></MediaId>?
<MsgId>1234567890123456</MsgId>?
</xml>?
*/?
public?function?responseMsg(){?
//獲取微信服務(wù)器POST請求中的數(shù)據(jù)?
$postStr?=?$GLOBALS["HTTP_RAW_POST_DATA"];?
if(?!empty($postStr)?){?
$postObj?=?simplexml_load_string($postStr,?'SimpleXMLElement',?LIBXML_NOCDATA);?
$fromUser?=?$postObj->FromUserName;?
$toUser?=?$postObj->ToUserName;?
$time?=?time();?
$msgType=?$postObj->MsgType;?
$picUrl?=?$postObj->PicUrl;?
$mediaId?=?$postObj->MediaId;?
$template?=?"<xml>?
<ToUserName><![CDATA[%s]]></ToUserName>?
<FromUserName><![CDATA[%s]]></FromUserName>?
<CreateTime>%s</CreateTime>?
<MsgType><![CDATA[%s]]></MsgType>?
<Content><![CDATA[%s]]></Content>?
</xml>";?
if(?strtolower($msgType)!='image'?){?
$msgType?=?"text";?
$content?=?"我只接受圖片消息";?
}else{?
$msgType?=?"text";?
if(?!empty(?$picUrl?)?){?
$content?=?"圖片鏈接為:".$picUrl."\n";?
$content?.=?"媒體id:".$mediaId;?
}else{?
$content?=?"請發(fā)送圖片";//消息為空?
}?
}?
$info?=?sprintf($template,?$fromUser,?$toUser,?$time,?$msgType,?$content);?
echo?$info;?
}else{?
echo?"";?
exit;?
}?
}?
}</span>

以上是小編跟大家分享的微信訊息自動回覆下所遇到的坑的相關(guān)知識,希望對大家有幫助!

以上是PHP微信開發(fā)之微信訊息自動回覆遇到的問題的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72