做个广告先,
PHP千人群(6848027)
C++群 (1414577)
看雪汇编&反汇编群(15375777)
看雪汇编&反汇编2群(4915800)
转载不一定注明出处,只要推荐下群就可以哈,各位亲~
微信开发_微信教程__微信通讯框架V1.0
<?php /** * 微信消息框架 by 牛牛 Q 184377367 */ class WeChat { public $token = null; public $msgType = null; public $event = null; public $eventKey = null; public $keyword = null; public $context = array(); public $insHandle = array(); public function Valid($retstr) { if ($this->CheckSignatrue()) { echo $retstr; exit; } } public function RegisterHandle(WeChatInterface $ins){ array_push($this->insHandle, $ins); } public function GetPostContent() { //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); $this->context['fromUserName'] = $postObj->FromUserName; $this->context['toUserName'] = $postObj->ToUserName; $this->context['time'] = $postObj->CreateTime; $this->keyword = trim($postObj->Content); $this->msgType = $postObj->MsgType; // $this->msgType = 'event'; $this->event = $postObj->Event; $this->eventKey = $postObj->EventKey; $this->ActionRouter(); } else { echo ""; exit; } } private function CheckSignatrue() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $tmpArr = array($this->token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if ($tmpStr == $signature) { return true; } else { return false; } } /* * 事件消息 */ public function OnEvent($event, $eventkey, $context ){ if ( count($this->insHandle)>0 ){ foreach ( $this->insHandle as $ins ){ $ins->OnEvent($event, $eventkey, $context); } } } /* * 文字消息 */ public function OnText($keyword,$context){ if ( count($this->insHandle)>0 ){ foreach ( $this->insHandle as $ins ){ $ins->OnText($keyword,$context); } } } private function ActionRouter(){ switch ($this->msgType) { case 'event': $this->OnEvent($this->event, $this->eventKey, $this->context); break; case 'text': $this->OnText($this->keyword, $this->context); break; default : exit(); } } } ?>
<?php /** * 微信消息框架 by 牛牛 Q 184377367 */ interface WeChatInterface{ public function OnEvent($event, $eventkey, $context); public function OnText($keyword,$context); } ?>
<?php /** * 微信消息框架 by 牛牛 Q 184377367 * 操作类必须实现 * WeChatInterface接口中的方法(OnEvent,OnText) * OnEvent * OnText * 微信有事件消息触发OnEvent,文本消息触发OnText */ class WeChatIns implements WeChatInterface { public function OnEvent($event, $eventkey, $context) { $this->PostNews($context); } public function OnText($keyword, $context) { $this->PostNews($context); } public function PostText(){ } public function PostNews($context){ $ary[0]['Title'] = '测试'; $ary[0]['Description'] = '测试'; $ary[0]['PicUrl'] = 'http://xxx.jpg'; $ary[0]['Url'] = 'http://qq.com'; echo WeChatUtil::CreateNewsXML($ary, $context); } } ?>
<?php /** * 微信消息框架 by 牛牛 Q 184377367 */ class WeChatUtil { static public function CreateNewsXML( $ary ,$context) { $count = count($ary); $textTpl = "<xml> <ToUserName><![CDATA[".$context['fromUserName']."]]></ToUserName> <FromUserName><![CDATA[".$context['toUserName']."]]></FromUserName> <CreateTime>".time()."</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[这里是内容]]></Content> <ArticleCount>".$count."</ArticleCount> <Articles>"; foreach($ary as $k=>$v){ $textTpl.="<item> <Title><![CDATA[".$v['Title']."]]></Title> <Description><![CDATA[".$v['Description']."]]> </Description> <PicUrl><![CDATA[".$v['PicUrl']."]]></PicUrl> <Url><![CDATA[".$v['Url']."]]></Url> </item>"; } $textTpl.="</Articles><FuncFlag>0</FuncFlag></xml>"; return $textTpl; } } ?>
<?php /** * 微信消息框架 by 牛牛 Q 184377367 */ /* import 是TP函数,换成include吧 0 0 */ import('ORG.WeChatInterface'); import('ORG.WeChatIns'); import('ORG.WeChat'); $wechatObj = new WeChat(); //微信TOKEN $wechatObj->token = ''; $wechatObj->RegisterHandle( new WeChatIns() ); if (isset( $_GET['echostr'] )) { $wechatObj->Valid( $_GET['echostr'] ); } else { $wechatObj->GetPostContent(); } ?>