<?php
namespace app\index\controller;
use think\Controller;
class index extends Controller
{
public function index(){
//获得参数 signature nonce token timestamp echostr
$nonce = $_GET['nonce'];
$token = 'imooc';
$timestamp = $_GET['timestamp'];
$echostr = isset($_GET['echostr'])?$_GET['echostr']:'';
$signature = $_GET['signature'];
//形成数组,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后与signature进行校验
$str = sha1( implode( $array ) );
if( $str == $signature && $echostr ){
//第一次接入weixin api接口的时候
echo $echostr;
exit;
}else{
$this->reponseMsg();
}
}
// 接收事件推送并回复
public function reponseMsg(){
//获取微信推送过来的数据(xml格式)
$postArr = $GLOBALS["HTTP_RAW_POST_DATA"];
$tmpstr = $postArr;
//处理消息,并设置回复类型
$postObj = simplexml_load_string($postArr);
//关注回复
if (strtolower($postObj->MsgType) =="event"){
//如果是关注事件
if(strtolower($postObj->Event) == "subscribe"){
//回复用户消息
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = "text";
$content = "欢迎关注我的公众号:\n 我的公众号"."\n\n微信用户的openid:\n".$postObj->FromUserName."\n\n如需关键字测试请输入:\n( 你好 || phone || composer )\n\n图文测试请输入:\n( 单图文 || 多图文 )";
$template = " <xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml> ";
$info = sprintf( $template,$toUser,$fromUser,$time,$msgType,$content);
echo $info;
}
}
//回复图文消息
if (strtolower($postObj->MsgType) == "text" && trim($postObj->Content == "单图文")){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$array = array(
array(
"title"=>"微博",
"description"=>"微博首页!",
"picUrl"=>"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg",
"url"=>"http://weibo.com/",
),
array(
"title"=>"微博2",
"description"=>"微博首页2!",
"picUrl"=>"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg",
"url"=>"http://weibo.com/",
),
array(
"title"=>"微博3",
"description"=>"微博首页3!",
"picUrl"=>"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg",
"url"=>"http://weibo.com/",
),
);
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($array)."</ArticleCount>
<Articles>";
foreach ($array as $key => $v) {
$template .= "<item>
<Title><![CDATA[".$v['title']."]]></Title>
<Description><![CDATA[".$v['description']."]]></Description>
<PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
<Url><![CDATA[".$v['url']."]]></Url>
</item>";
}
$template.= "</Articles></xml>";
$time = time();
echo sprintf( $template,$toUser,$fromUser,$time,"news");
}elseif (strtolower($postObj->MsgType) == "text" && trim($postObj->Content == "多图文")) {
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$array = array(
array(
"title"=>"腾讯网",
"description"=>"腾讯首页!",
"picUrl"=>"图片url地址",
"url"=>"http://weibo.com/",
),
array(
"title"=>"easywechat文档",
"description"=>"微信文档!",
"picUrl"=>"图片url地址",
"url"=>"http://weibo.com/",
),
array(
"title"=>"laravel学院",
"description"=>"学院君!",
"picUrl"=>"图片url地址",
"url"=>"http://weibo.com/",
),
);
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($array)."</ArticleCount>
<Articles>";
foreach ($array as $key => $v) {
$template .= "<item>
<Title><![CDATA[".$v['title']."]]></Title>
<Description><![CDATA[".$v['description']."]]></Description>
<PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
<Url><![CDATA[".$v['url']."]]></Url>
</item>";
}
$template.= "</Articles></xml>";
$time = time();
echo sprintf( $template,$toUser,$fromUser,$time,"news");
}else{
switch ($postObj->Content) {
case '你好':
$content = "hello 你好!";
break;
case 'phone':
$content = "我的手机号码:\n12339475";
break;
case 'php':
$content = "我是php";
break;
case 'composer':
$content = "<a href='http://www.baidu.com'>composer</a>";
break;
default:
$content = '无法匹配';
break;
}
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = "text";
echo sprintf( $template,$toUser,$fromUser,$time,$msgType,$content);
}
}
}
?>