php微信公眾號開發(fā)之關鍵詞回復
更新時間:2018年10月20日 09:00:11 作者:dq_095
這篇文章主要為大家詳細介紹了php微信公眾號開發(fā)之關鍵詞回復,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了php微信公眾號開發(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 = "公司簡介!";
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;
}
}
}
?>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
PHP統(tǒng)計nginx訪問日志中的搜索引擎抓取404鏈接頁面路徑
這篇文章主要介紹了PHP統(tǒng)計nginx訪問日志中的搜索引擎抓取404鏈接頁面路徑,可以對每個搜索引擎單獨統(tǒng)計,需要的朋友可以參考下2014-06-06
PHP 使用 Imagick 裁切/生成縮略圖/添加水印自動檢測和處理 GIF
這篇文章主要介紹了PHP 使用 Imagick 裁切/生成縮略圖/添加水印自動檢測和處理 GIF的相關資料,需要的朋友可以參考下2016-02-02
Ubuntu 16.04中Laravel5.4升級到5.6的步驟
這篇文章主要給大家介紹了關于在Ubuntu 16.04中Laravel5.4升級到5.6的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2018-12-12

