微信监听到事件后回调到到我们公众号配置好的接口地址
/*** 配置Token!!!*/public function home(){$signature = $_GET["signature"];$timestamp = $_GET["timestamp"];$nonce = $_GET["nonce"];$token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr, SORT_STRING);$tmpStr = implode($tmpArr);$tmpStr = sha1($tmpStr);$echoStr = $_GET["echostr"];if ($tmpStr == $signature) {$this->responseMsg(); } else {header('content-type:text');ob_clean();echo $echoStr;}exit(); }
首次关注
/*** 发送消息*/public function responseMsg(){$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;}if ($this->res['Event'] == 'CLICK') {//点击事件if ($this->res['EventKey'] == 'TO_IMG') {//图片$this->sendImg("6LGwaGlcL1nEFVNHiWH2YQv4ztDUvsq-7WTAhvUkRU4");}}}}
回复关注文字消息:
/*** 发送关注消息* @param $content*/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>";}
回复图片消息:(永久)参考:微信公众号(四)——上传永久素材(图片)
/*** 回复图片* @param $content*/public function sendImg($content){echo "<xml><ToUserName><![CDATA[".$this->res['FromUserName']."]]></ToUserName><FromUserName><![CDATA[".$this->res['ToUserName']."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[".$content."]]></MediaId></Image></xml>";}