PHP微信關注自動回復文本消息。


服務器配置URL默認接受 $_GET["echostr"] 配置成功。

public function GetShow(){
$token = $this->token;
//將token、timestamp、nonce三個參數進行字典序排序
$arr = [$token,$_GET["timestamp"],$_GET['nonce']];
sort($arr);
$str = implode($arr);
//加密
$GetShow = sha1($str);
//加密后的字符串與signature對比
if($GetShow == $_GET["signature"]){
echo $_GET['echostr'];die;
}
}

/*用戶關注授權獲取信息*/
public function responseMsg(){
if(@!empty($_GET["echostr"])){
file_put_contents("1.txt",json_encode($_GET));
$this->GetShow();
}
$data = file_get_contents("php://input");
$this->res = (array)simplexml_load_string($data,"SimpleXMLElement",LIBXML_NOCDATA);
//判斷是否首次關注
if ($this->res['MsgType'] == 'event')
{
if ($this->res['Event'] == 'subscribe')
{
$this->sendText("歡迎您關注我們,更多了解,敬請期待!");
}
if ($this->res['Event'] == 'unsubscribe')
{
echo "取消關注";die;
}
}
}
//公共號首次關注推送消息
public function sendText($content){
echo "<xml>
<ToUserName><![CDATA[".$this->res['FromUserName']."]]></ToUserName>
<FromUserName><![CDATA[".$this->res['ToUserName']."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[".$content."]]></Content>
</xml>";
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM