无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了Yii Framework框架开发微信公众平台。分享给大家供大家参考,具体如下:
1. 先到微信公众平台注册帐号
http://mp.weixin.qq.com
2. 下载demo
微信公众平台提供了一个十分“朴素”的demo,说明如何调用消息接口的。代码真的很朴素,具体内容可到官网下载。
3. 按照Yii的规则,做一个extension。
这里命名为 weixin,目录结构如下:
"htmlcode">
<"http://www.sharefamily.net" rel="external nofollow" target="_blank">http://www.sharefamily.net</a>} */ class Weixin { //$_GET参数 public $signature; public $timestamp; public $nonce; public $echostr; // public $token; public $debug = false; public $msg = array(); public $setFlag = false; /** * __construct * * @param mixed $params * @access public * @return void */ public function __construct($params) { foreach ($params as $k1 => $v1) { if (property_exists($this, $k1)) { $this->$k1 = $v1; } } } /** * valid * * @access public * @return void */ public function valid() { //valid signature , option if($this->checkSignature()){ echo $this->echostr; Yii::app()->end(); } } /** * 获得用户发过来的消息(消息内容和消息类型 ) * * @access public * @return void */ public function init() { $postStr = empty($GLOBALS["HTTP_RAW_POST_DATA"]) "HTTP_RAW_POST_DATA"]; if ($this->debug) { $this->log($postStr); } if (!empty($postStr)) { $this->msg = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); } } /** * makeEvent * * @access public * @return void */ public function makeEvent() { } /** * 回复文本消息 * * @param string $text * @access public * @return void */ public function makeText($text='') { $createTime = time(); $funcFlag = $this->setFlag "<xml> <ToUserName><![CDATA[{$this->msg->FromUserName}]]></ToUserName> <FromUserName><![CDATA[{$this->msg->ToUserName}]]></FromUserName> <CreateTime>{$createTime}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%s</FuncFlag> </xml>"; return sprintf($textTpl,$text,$funcFlag); } /** * 根据数组参数回复图文消息 * * @param array $newsData * @access public * @return void */ public function makeNews($newsData=array()) { $createTime = time(); $funcFlag = $this->setFlag "<xml> <ToUserName><![CDATA[{$this->msg->FromUserName}]]></ToUserName> <FromUserName><![CDATA[{$this->msg->ToUserName}]]></FromUserName> <CreateTime>{$createTime}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount><Articles>"; $newTplItem = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $newTplFoot = "</Articles> <FuncFlag>%s</FuncFlag> </xml>"; $content = ''; $itemsCount = count($newsData['items']); //微信公众平台图文回复的消息一次最多10条 $itemsCount = $itemsCount < 10 "\n\r", FILE_APPEND); } } }使用方法,这里举例在SiteController里面
/** * actionIndex * * @access public * @return void */ public function actionIndex() { $weixin = new Weixin($_GET); $weixin->token = $this->_weixinToken; //$weixin->debug = true; //网址接入时使用 if (isset($_GET['echostr'])) { $weixin->valid(); } $weixin->init(); $reply = ''; $msgType = empty($weixin->msg->MsgType) "_blank" href="//www.jb51.net/Special/386.htm">Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分轨】
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]