这篇文章主要介绍了如何在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',
'百度','', 'https://cache.yisu.com/upload/information/20201209/266/40593.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;
}
}
}
感谢你能够认真阅读完这篇文章,希望小编分享的“如何在SAE环境下搭建CI框架”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。