微信公众平台DEMO(PHP)
发布:smiling 来源: PHP粉丝网 添加日期:2019-07-30 11:28:45 浏览: 评论:0
本人在SAE环境下搭建了CI框架(其实这个小东西用不着用框架的),直接把代码写在了控制器里面。
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- //define your token
- define("TOKEN", "109");
- class Welcome extends CI_Controller {
- public function index()
- {
- /*
- $this->load->helper('url');
- $this->load->view('welcome_message');
- */
- // use chat response
- $this->responseMsg();
- }
- // chat response
- public function responseMsg()
- {
- //get post data, May be due to the different environments
- $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
- //extract post data
- if (!emptyempty($postStr)){
- $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- $fromUsername = $postObj->FromUserName;
- $toUsername = $postObj->ToUserName;
- $MsgType = $postObj->MsgType;
- $time = time();
- switch($MsgType){
- case('text'):
- $keyword = trim($postObj->Content);
- $textTpl = "<xml>
- <tousername><!--[CDATA[%s]]--></tousername>
- <fromusername><!--[CDATA[%s]]--></fromusername>
- <createtime>%s</createtime>
- <msgtype><!--[CDATA[%s]]--></msgtype>
- <content><!--[CDATA[%s]]--></content>
- </xml>";
- switch($keyword){
- case(1):
- // Need to optimize
- // random read data from jokes
- $sql = 'SELECT * FROM jokes';
- $query = $this->db->query($sql);
- $res = $query->result_array();
- $num_rows = $query->num_rows();
- $key = rand(0, $num_rows - 1); // Notice: The value of key is from 0.
- //$contentStr = $key.'#'.$res[$key ]['content']; // debug
- $contentStr = $res[$key ]['content'];
- break;
- case(2):
- $contentStr = 'Your fromUsername is: '.$fromUsername;
- break;
- case(3):
- $newsTpl = "<xml>
- <tousername><!--[CDATA[%s]]--></tousername>
- <fromusername><!--[CDATA[%s]]--></fromusername>
- <createtime>%s</createtime>
- <msgtype><!--[CDATA[%s]]--></msgtype>
- <articlecount>2</articlecount>
- <articles>
- <item>
- <title><![CDATA[%s]]></title>
- <description><!--[CDATA[%s]]--></description>
- <picurl><!--[CDATA[%s]]--></picurl>
- <url><!--[CDATA[%s]]--></url>
- </item>
- <item>
- <title><![CDATA[%s]]></title>
- <description><!--[CDATA[%s]]--></description>
- <picurl><!--[CDATA[%s]]--></picurl>
- <url><!--[CDATA[%s]]--></url>
- </item>
- </articles>
- </xml> ";
- $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time,'news',
- '百度','', 'http://www.baidu.com/img/bdlogo.gif', 'http://www.baidu.com',
- 'Google','', '', 'http://www.google.com'); // Notice: Google's logo is not suitable.
- echo $resultStr;
- exit; // Notice: It's exit, not break.
- case(4):
- $contentStr = "该功能正在开发中,敬请期待...";
- break;
- /* others */
- default:
- $contentStr = "回复数字 选择服务\n";
- $contentStr .= "1 笑话精选
- ";
- $contentStr .= "2 获取您的Username...\n";
- $contentStr .= "3 图文消息示例\n";
- $contentStr .= "4 开发中...\n";
- break;
- }
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
- break;
- case('image'):
- $media_id = $postObj->MediaId;
- $imgTpl = "<xml>
- <tousername><!--[CDATA[%s]]--></tousername>
- <fromusername><!--[CDATA[%s]]--></fromusername>
- <createtime>%s</createtime>
- <msgtype><!--[CDATA[%s]]--></msgtype>
- <img>
- <mediaid><!--[CDATA[%s]]--></mediaid>
- </xml>";
- $resultStr = sprintf($imgTpl, $fromUsername, $toUsername, $time, 'image', $media_id);
- break;
- // try get the id of the receive image and analyse
- /*
- $media_id = $postObj->MediaId;
- $textTpl = "<xml>
- <tousername><!--[CDATA[%s]]--></tousername>
- <fromusername><!--[CDATA[%s]]--></fromusername>
- <createtime>%s</createtime>
- <msgtype><!--[CDATA[%s]]--></msgtype>
- <content><!--[CDATA[%s]]--></content>
- </xml>";
- $length = strlen($media_id);
- $contentStr = "I have received the image message you sent, the id of this image is # $media_id #, and the length of media_id is # $length #";
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
- break;
- */
- case('voice'):
- $textTpl = "<xml>
- <tousername><!--[CDATA[%s]]--></tousername>
- <fromusername><!--[CDATA[%s]]--></fromusername>
- <createtime>%s</createtime>
- <msgtype><!--[CDATA[%s]]--></msgtype>
- <content><!--[CDATA[%s]]--></content>
- </xml>";
- $contentStr = '你说啥?俺听不见...';
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
- break;
- /* others */
- default:
- $resultStr = "Input something...";
- break;
- //phpfensi.com
- }
- echo $resultStr;
- }else {
- echo "";
- exit;
- }
- }
- }
Tags: 微信公众平台DEMO
- 上一篇:php遍历解析xml字符串的方法
- 下一篇:PHP实现的限制IP投票程序IP来源分析
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)