本人在SAE环境下搭建了CI框架(其实这个小东西用不着用框架的),直接把代码写在了控制器里面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
<?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 ; } } } |