微信公众平台DEMO(PHP)
程序员文章站
2024-04-02 11:05:28
本人在sae环境下搭建了ci框架(其实这个小东西用不着用框架的),直接把代码写在了控制器里面
本人在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 (!empty($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 笑话精选\n"; $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> <image> <mediaid><![cdata[%s]]></mediaid> </image> </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; } echo $resultstr; }else { echo ""; exit; } } }