php微信公眾號(hào)開發(fā)之關(guān)鍵詞回復(fù)
本文實(shí)例為大家分享了php微信公眾號(hào)開發(fā)之關(guān)鍵詞回復(fù)的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo):
- 消息回復(fù)
- 關(guān)鍵詞回復(fù)
- utf8編碼

index.php
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "jiekou");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
switch ($keyword)
{
case "1";
$contentStr = "公司簡(jiǎn)介!";
break;
case "2";
$contentStr = "最新優(yōu)惠!";
break;
default;
$contentStr = "歡迎光臨!";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
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;
}
}
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
thinkPHP簡(jiǎn)單導(dǎo)入和使用阿里云OSSsdk的方法
這篇文章主要介紹了thinkPHP簡(jiǎn)單導(dǎo)入和使用阿里云OSSsdk的方法,簡(jiǎn)單說明了阿里云OSS的php sdk下載地址及thinkPHP導(dǎo)入與使用OSSsdk的方法,需要的朋友可以參考下2017-03-03
yii實(shí)現(xiàn)使用CUploadedFile上傳文件的方法
這篇文章主要介紹了yii實(shí)現(xiàn)使用CUploadedFile上傳文件的方法,結(jié)合具體的前端與后端處理代碼實(shí)例分析了CUploadedFile類的使用方法,需要的朋友可以參考下2015-12-12
ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作示例
這篇文章主要介紹了ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作,結(jié)合實(shí)例形式分析了thinkPHP5使用build自動(dòng)生成模塊的具體步驟、方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-04-04
ThinkPHP模板替換與系統(tǒng)常量及應(yīng)用實(shí)例教程
這篇文章主要介紹了ThinkPHP模板替換與系統(tǒng)常量及應(yīng)用,是很重要的概念,需要的朋友可以參考下2014-08-08
PHP實(shí)現(xiàn)加減乘除最簡(jiǎn)單的實(shí)例分享
在本篇文章里小編給大家整理了一篇關(guān)于PHP實(shí)現(xiàn)簡(jiǎn)單的加減乘除的實(shí)例內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。2021-08-08
PHP超全局?jǐn)?shù)組(Superglobals)介紹
這篇文章主要介紹了PHP超全局?jǐn)?shù)組(Superglobals)介紹,本文講解了概述、變量的作用域、超全局?jǐn)?shù)組及注意事項(xiàng)等內(nèi)容,需要的朋友可以參考下2015-07-07

