public function __construct() { //加载站点缓存 service('site')->loadSite(); //与微信官网通信绑定验证 \Weixin::valid(); $this->instance = \Weixin::instance('message'); }
/** * default action 'index' * * @param Request $request * @param Response $response */ public function index(Request $request, Response $response) { $t = $request->arg(1); $wx = new Weixin([], $t); if (!isset($_GET['echostr'])) { if ($wx->checkSignature()) { //签名检测 $wx->responseMsg(); } else { echo ''; } } else { //接口验证 $wx->valid(); } exit; }
public function index() { import('Think.ORG.Weixin'); // 导入微信类 $wechat = new Weixin(); $wechat->valid(); //$wechat->responseMsg(); $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { $key_word = M('keyword'); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = trim($postObj->FromUserName); //发送方帐号(一个OpenID) $toUsername = $postObj->ToUserName; //开发者微信号 $keyword = trim($postObj->Content); //用户发来的信息 $RX_TYPE = trim($postObj->MsgType); //类型 $EventKey = trim($postObj->EventKey); //事件KEY值 $Event = $postObj->Event; //事件类型 $time = time(); if ($fromUsername != '') { $user = M('user')->field('id,username')->where("username='******'")->find(); if ($user) { } else { $date = time(); $data['username'] = $fromUsername; //用户名 $data['reg_time'] = $date; $data['last_time'] = $date; $userid = M('user')->add($data); } } if ($RX_TYPE == 'event') { //**自定义点击事件**// if ($Event == 'CLICK') { if ($EventKey != '') { $where = array('key' => $EventKey); $custom_key = M('custom_menu')->where($where)->find(); $key_list = $key_word->where("kyword='" . $custom_key['keyword'] . "'")->find(); //$key_list= $key_word->where("kyword='".$EventKey."'")->find(); if (is_array($key_list)) { if ($key_list['type'] == 1) { $this->wenben($fromUsername, $toUsername, $time, $key_list['kecontent']); } else { $titles = unserialize($key_list['titles']); $imageinfo = unserialize($key_list['imageinfo']); $linkinfo = unserialize($key_list['linkinfo']); $textTpl = "<xml>\r\n\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\r\n\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\r\n\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\r\n\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\r\n\t\t\t\t\t\t\t <ArticleCount>%s</ArticleCount> \r\n <Articles>"; for ($i = 0; $i < count($titles); $i++) { if (stristr($linkinfo[$i], $_SERVER['SERVER_NAME'])) { if (stristr($linkinfo[$i], "?")) { $links = $linkinfo[$i] . "&key=" . $fromUsername; } else { $links = $linkinfo[$i] . "?key=" . $fromUsername; } } else { $links = $linkinfo[$i]; } if (stristr($imageinfo[$i], $_SERVER['SERVER_NAME'])) { $images = $imageinfo[$i]; } else { $images = "http://" . $_SERVER['SERVER_NAME'] . __ROOT__ . '/' . $imageinfo[$i]; } $textTpl .= " <item>\r\n <Title><![CDATA[" . $titles[$i] . "]]></Title> \r\n <Description><![CDATA[" . $titles[$i] . "]]></Description>\r\n <PicUrl><![CDATA[" . $images . "]]></PicUrl>\r\n <Url><![CDATA[" . $links . "]]></Url>\r\n </item>"; /* $textTpl.= " <item> <Title><![CDATA[".$titles[$i]."]]></Title> <Description><![CDATA[".$titles[$i]."]]></Description> <PicUrl><![CDATA[http://".$_SERVER['SERVER_NAME'].__ROOT__.'/'.$imageinfo[$i]."]]></PicUrl> <Url><![CDATA[".$links."]]></Url> </item>";*/ } $textTpl .= "</Articles>\r\n <FuncFlag>0</FuncFlag>\r\n </xml> \r\n\t\t\t\t\t\t\t"; $this->tuwen($textTpl, $fromUsername, $toUsername, $time, count($titles)); } } } } if ($Event == 'subscribe') { $key_list = $key_word->where("isfollow=1")->find(); if (is_array($key_list)) { if ($key_list['type'] == 1) { $this->wenben($fromUsername, $toUsername, $time, $key_list['kecontent']); } else { $titles = unserialize($key_list['titles']); $imageinfo = unserialize($key_list['imageinfo']); $linkinfo = unserialize($key_list['linkinfo']); $textTpl = "<xml>\r\n\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\r\n\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\r\n\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\r\n\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\r\n\t\t\t\t\t\t\t <ArticleCount>%s</ArticleCount> \r\n <Articles>"; for ($i = 0; $i < count($titles); $i++) { if (stristr($linkinfo[$i], $_SERVER['SERVER_NAME'])) { if (stristr($linkinfo[$i], "?")) { $links = $linkinfo[$i] . "&key=" . $fromUsername; } else { $links = $linkinfo[$i] . "?key=" . $fromUsername; } } else { $links = $linkinfo[$i]; } if (stristr($imageinfo[$i], $_SERVER['SERVER_NAME'])) { $images = $imageinfo[$i]; } else { $images = "http://" . $_SERVER['SERVER_NAME'] . __ROOT__ . '/' . $imageinfo[$i]; } $textTpl .= " <item>\r\n <Title><![CDATA[" . $titles[$i] . "]]></Title> \r\n <Description><![CDATA[" . $titles[$i] . "]]></Description>\r\n <PicUrl><![CDATA[" . $images . "]]></PicUrl>\r\n <Url><![CDATA[" . $links . "]]></Url>\r\n </item>"; } $textTpl .= "</Articles>\r\n <FuncFlag>0</FuncFlag>\r\n </xml> \r\n\t\t\t\t\t\t\t"; $this->tuwen($textTpl, $fromUsername, $toUsername, $time, count($titles)); } } } } if (!empty($keyword)) { $key_list = $key_word->where("kyword='" . $keyword . "'")->find(); if (is_array($key_list)) { if ($key_list['type'] == 1) { $this->wenben($fromUsername, $toUsername, $time, $key_list['kecontent']); } else { $titles = unserialize($key_list['titles']); $imageinfo = unserialize($key_list['imageinfo']); $linkinfo = unserialize($key_list['linkinfo']); $textTpl = "<xml>\r\n\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\r\n\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\r\n\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\r\n\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\r\n\t\t\t\t\t\t\t <ArticleCount>%s</ArticleCount> \r\n <Articles>"; for ($i = 0; $i < count($titles); $i++) { if (stristr($linkinfo[$i], $_SERVER['SERVER_NAME'])) { if (stristr($linkinfo[$i], "?")) { $links = $linkinfo[$i] . "&key=" . $fromUsername; } else { $links = $linkinfo[$i] . "?key=" . $fromUsername; } } else { $links = $linkinfo[$i]; } if (stristr($imageinfo[$i], $_SERVER['SERVER_NAME'])) { $images = $imageinfo[$i]; } else { $images = "http://" . $_SERVER['SERVER_NAME'] . __ROOT__ . '/' . $imageinfo[$i]; } $textTpl .= " <item>\r\n <Title><![CDATA[" . $titles[$i] . "]]></Title> \r\n <Description><![CDATA[" . $titles[$i] . "]]></Description>\r\n <PicUrl><![CDATA[" . $images . "]]></PicUrl>\r\n <Url><![CDATA[" . $links . "]]></Url>\r\n </item>"; } $textTpl .= "</Articles>\r\n <FuncFlag>0</FuncFlag>\r\n </xml> \r\n\t\t\t\t\t\t\t"; $this->tuwen($textTpl, $fromUsername, $toUsername, $time, count($titles)); } } else { $key_list = $key_word->where("ismess=1")->find(); if (is_array($key_list)) { if ($key_list['type'] == 1) { $this->wenben($fromUsername, $toUsername, $time, $key_list['kecontent']); } else { $titles = unserialize($key_list['titles']); $imageinfo = unserialize($key_list['imageinfo']); $linkinfo = unserialize($key_list['linkinfo']); $textTpl = "<xml>\r\n\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\r\n\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\r\n\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\r\n\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\r\n\t\t\t\t\t\t\t <ArticleCount>%s</ArticleCount> \r\n <Articles>"; for ($i = 0; $i < count($titles); $i++) { if (stristr($linkinfo[$i], $_SERVER['SERVER_NAME'])) { if (stristr($linkinfo[$i], "?")) { $links = $linkinfo[$i] . "&key=" . $fromUsername; } else { $links = $linkinfo[$i] . "?key=" . $fromUsername; } } else { $links = $linkinfo[$i]; } if (stristr($imageinfo[$i], $_SERVER['SERVER_NAME'])) { $images = $imageinfo[$i]; } else { $images = "http://" . $_SERVER['SERVER_NAME'] . __ROOT__ . '/' . $imageinfo[$i]; } $textTpl .= " <item>\r\n <Title><![CDATA[" . $titles[$i] . "]]></Title> \r\n <Description><![CDATA[" . $titles[$i] . "]]></Description>\r\n <PicUrl><![CDATA[" . $images . "]]></PicUrl>\r\n <Url><![CDATA[" . $links . "]]></Url>\r\n </item>"; } $textTpl .= "</Articles>\r\n <FuncFlag>0</FuncFlag>\r\n </xml> \r\n\t\t\t\t\t\t\t"; $this->tuwen($textTpl, $fromUsername, $toUsername, $time, count($titles)); } } else { } } } else { echo "Input something..."; } } else { echo ""; exit; } }
exit('No direct script access allowed'); } /* 微信接口文档 /* +----------------------------------------------------------------------+ /* | PHP version sam 1.0.0 /* +----------------------------------------------------------------------+ /* | Copyright (c) 2013-2015 SAM /* +----------------------------------------------------------------------+ /* | Authors: King /* +----------------------------------------------------------------------+ /* 目前不执行微信加密接口,请使用明文模式 /* +----------------------------------------------------------------------+ */ define("TOKEN", "weixin"); $wechatObj = new Weixin(); $wechatObj->valid(); class Weixin extends CI_Controller { public function __construct() { parent::__construct(); //载入weixin_model模型 $this->load->model('weixin_model'); //当前的域名 $this->domain = "http://" . $_SERVER['SERVER_NAME'] . "/" . "index.php/"; } // 微信参数验证 public function valid() { $echoStr = htmlspecialchars($_GET["echostr"]); if ($this->checkSignature()) {