微信公眾平臺DEMO(PHP)
更新時(shí)間:2016年05月04日 10:22:29 作者:燕玉苗
這篇文章主要介紹了微信公眾平臺DEMO(PHP),需要的朋友可以參考下
本人在SAE環(huán)境下搭建了CI框架(其實(shí)這個(gè)小東西用不著用框架的),直接把代碼寫在了控制器里面
<?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 = "該功能正在開發(fā)中,敬請期待..."; break; /* others */ default: $contentStr = "回復(fù)數(shù)字 選擇服務(wù)\n"; $contentStr .= "1 笑話精選\n"; $contentStr .= "2 獲取您的Username...\n"; $contentStr .= "3 圖文消息示例\n"; $contentStr .= "4 開發(fā)中...\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; } } }
您可能感興趣的文章:
- 拋棄 PHP 代價(jià)太高
- 100行PHP代碼實(shí)現(xiàn)socks5代理服務(wù)器
- win7下搭建nginx+php的開發(fā)環(huán)境
- PHP.vs.JAVA
- java解析php函數(shù)json_encode unicode 編碼問題
- wdcp添加tomcat,同時(shí)支持php和java教程
- java URL 獲取PHP JSON 數(shù)據(jù)
- PHPExcel筆記, mpdf導(dǎo)出
- php面向?qū)ο笾祮卫J?/a>
- PHP中的.htaccess偽靜態(tài)文件
- Centos安裝php高版本
- 100多行PHP代碼實(shí)現(xiàn)socks5代理服務(wù)器[2]
- win10環(huán)境PHP 7 安裝配置【教程】
相關(guān)文章
PHP將整個(gè)網(wǎng)站生成HTML純靜態(tài)網(wǎng)頁的方法總結(jié)
我經(jīng)常會在網(wǎng)上看到有人問怎么將整個(gè)動態(tài)的網(wǎng)站靜態(tài)化,其實(shí)實(shí)現(xiàn)的方法很簡單2012-02-02如何使用Serializable接口來自定義PHP中類的序列化
這篇文章主要介紹了如何使用Serializable接口來自定義PHP中類的序列化,幫助大家更好的理解和學(xué)習(xí)使用PHP,感興趣的朋友可以了解下2021-04-04