200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > php关键词回复源码 PHP对接微信公众号实现简单自动回复

php关键词回复源码 PHP对接微信公众号实现简单自动回复

时间:2019-12-01 16:08:58

相关推荐

php关键词回复源码 PHP对接微信公众号实现简单自动回复

require_once "defs.php";

function index()

{

$timestamp = $_GET['timestamp'];

$nonce = $_GET['nonce'];

$token = 'hongming'; //公众号里面配置的token

$signature = $_GET['signature'];

$echostr = $_GET['echostr']; //每一次都要验证, 只有第一次验证才会有 echostr

$array = array( $timestamp, $nonce, $token);

sort( $array );

//2.将排序后的三个参数拼接之后用sha1加密

$tempstr = implode('', $array);

$tempstr = sha1( $tempstr );

//3.将加密后的字符串与signature进行对比,判断该请求是否来自微信

if( $tempstr == $signature && $echostr){ //启动服务器配置 会进入到这里

echo $_GET['echostr'];

exit();

}else{

reponseMsg(); //这里是 启用成功后, 接受事件用的

}

}

function reponseMsg(){

$postStr = file_get_contents("php://input");

//根据事件,进行自己的逻辑代码吧 请开始你的表演

if (!empty($postStr)){

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$fromUsername = $postObj->FromUserName;

$toUsername = $postObj->ToUserName;

$msgtype = $postObj->MsgType;

$keyword = trim($postObj->Content);

$time = time();

$textTpl = "

%s

0

";

if(!empty( $keyword ) & $msgtype == "text") //文本判断

{

$msgType = "text";

$contentStr = sousuo($keyword);

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}

if($msgtype=='event'){ // 关注判断

$msgType = "text";

$contentStr = "感谢您的关注!";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}

else // 其他类型消息判断

{

$msgType = "text";

$contentStr = "回复关键词可以查询影视资源哦!";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}

}

}

index();

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。