公众平台DEMO(PHP)本⼈在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
如何搭建php开发环境$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',
'百度','', 'www.baidu/img/bdlogo.gif', 'www.baidu',
'Google','', '', 'le'); // 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 获取您的\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 = ""; break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论