Yii Framework框架开发微信公众平台示例
本文实例讲述了YiiFramework框架开发微信公众平台。分享给大家供大家参考,具体如下:
1.先到微信公众平台注册帐号
http://mp.weixin.qq.com
2.下载demo
微信公众平台提供了一个十分“朴素”的demo,说明如何调用消息接口的。代码真的很朴素,具体内容可到官网下载。
3.按照Yii的规则,做一个extension。
这里命名为weixin,目录结构如下:
▾extensions/
▾weixin/
Weixin.php*
Weixin.php代码内容:
http://www.sharefamily.net} */ classWeixin { //$_GET参数 public$signature; public$timestamp; public$nonce; public$echostr; // public$token; public$debug=false; public$msg=array(); public$setFlag=false; /** *__construct * *@parammixed$params *@accesspublic *@returnvoid */ publicfunction__construct($params) { foreach($paramsas$k1=>$v1) { if(property_exists($this,$k1)) { $this->$k1=$v1; } } } /** *valid * *@accesspublic *@returnvoid */ publicfunctionvalid() { //validsignature,option if($this->checkSignature()){ echo$this->echostr; Yii::app()->end(); } } /** *获得用户发过来的消息(消息内容和消息类型) * *@accesspublic *@returnvoid */ publicfunctioninit() { $postStr=empty($GLOBALS["HTTP_RAW_POST_DATA"])?'':$GLOBALS["HTTP_RAW_POST_DATA"]; if($this->debug) { $this->log($postStr); } if(!empty($postStr)){ $this->msg=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA); } } /** *makeEvent * *@accesspublic *@returnvoid */ publicfunctionmakeEvent() { } /** *回复文本消息 * *@paramstring$text *@accesspublic *@returnvoid */ publicfunctionmakeText($text='') { $createTime=time(); $funcFlag=$this->setFlag?1:0; $textTpl=""; returnsprintf($textTpl,$text,$funcFlag); } /** *根据数组参数回复图文消息 * *@paramarray$newsData *@accesspublic *@returnvoid */ publicfunctionmakeNews($newsData=array()) { $createTime=time(); $funcFlag=$this->setFlag?1:0; $newTplHeader=" msg->FromUserName}]]> msg->ToUserName}]]> {$createTime} %s "; $content=''; $itemsCount=count($newsData['items']); //微信公众平台图文回复的消息一次最多10条 $itemsCount=$itemsCount<10?$itemsCount:10; if($itemsCount){ foreach($newsData['items']as$key=>$item){ if($key<=9){ $content.=sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']); } } } $header=sprintf($newTplHeader,$itemsCount); $footer=sprintf($newTplFoot,$funcFlag); return$header.$content.$footer; } /** *reply * *@parammixed$data *@accesspublic *@returnvoid */ publicfunctionreply($data) { if($this->debug) { $this->log($data); } echo$data; } /** *checkSignature * *@accessprivate *@returnvoid */ privatefunctioncheckSignature() { $tmpArr=array($this->token,$this->timestamp,$this->nonce); sort($tmpArr); $tmpStr=implode($tmpArr); $tmpStr=sha1($tmpStr); if($tmpStr==$this->signature){ returntrue; }else{ returnfalse; } } /** *log * *@accessprivate *@returnvoid */ privatefunctionlog($log) { if($this->debug) { file_put_contents(Yii::getPathOfAlias('application').'/runtime/weixin_log.txt',var_export($log,true)."\n\r",FILE_APPEND); } } } msg->FromUserName}]]> msg->ToUserName}]]> {$createTime} %s "; $newTplItem=" - "; $newTplFoot="
%s
使用方法,这里举例在SiteController里面
/** *actionIndex * *@accesspublic *@returnvoid */ publicfunctionactionIndex() { $weixin=newWeixin($_GET); $weixin->token=$this->_weixinToken; //$weixin->debug=true; //网址接入时使用 if(isset($_GET['echostr'])) { $weixin->valid(); } $weixin->init(); $reply=''; $msgType=empty($weixin->msg->MsgType)?'':strtolower($weixin->msg->MsgType); switch($msgType) { case'text': //你要处理文本消息代码 break; case'image': //你要处理图文消息代码 break; case'location': //你要处理位置消息代码 break; case'link': //你要处理链接消息代码 break; case'event': //你要处理事件消息代码 break; default: //无效消息情况下的处理方式 break; } $weixin->reply($reply); }
至此,基本的逻辑都实现了
更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。