您的位置:首页 > 移动开发 > 微信开发

微信公众平台开发(103) 四六级成绩查询

2014-10-21 11:06 441 查看
2014年6月全国大学英语四、六级考试考试成绩于2014年8月20日上午9时发布

我们提供微信查询方法

1、关注微信公众账号



二、回复“四六级”



三、输入姓名和准考证号



四、返回查询结果



代码实现

1 <?php
2 /*
3     方倍工作室 2014年6月全国大学英语四六级考试成绩查询
4     CopyRight 2014 All Rights Reserved
5 */
6
7 define("TOKEN", "weixin");
8
9 $wechatObj = new wechatCallbackapiTest();
10 if (!isset($_GET['echostr'])) {
11     $wechatObj->responseMsg();
12 }else{
13     $wechatObj->valid();
14 }
15
16 class wechatCallbackapiTest
17 {
18     public function valid()
19     {
20         $echoStr = $_GET["echostr"];
21         $signature = $_GET["signature"];
22         $timestamp = $_GET["timestamp"];
23         $nonce = $_GET["nonce"];
24         $token = TOKEN;
25         $tmpArr = array($token, $timestamp, $nonce);
26         sort($tmpArr);
27         $tmpStr = implode($tmpArr);
28         $tmpStr = sha1($tmpStr);
29         if($tmpStr == $signature){
30             echo $echoStr;
31             exit;
32         }
33     }
34
35     public function responseMsg()
36     {
37         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
38         if (!empty($postStr)){
39             $this->logger("R ".$postStr);
40             $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
41             $RX_TYPE = trim($postObj->MsgType);
42
43             switch ($RX_TYPE)
44             {
45                 case "event":
46                     $result = $this->receiveEvent($postObj);
47                     break;
48                 case "text":
49                     $result = $this->receiveText($postObj);
50                     break;
51             }
52             $this->logger("T ".$result);
53             echo $result;
54         }else {
55             echo "";
56             exit;
57         }
58     }
59
60     private function receiveEvent($object)
61     {
62         $content = "";
63         switch ($object->Event)
64         {
65             case "subscribe":
66                 $content = array();
67                 $content[] = array("Title" =>"2014年6月全国大学英语四六级考试成绩查询","Description" =>"", "PicUrl" =>"http://365jia.cn/uploads/13/0301/5130c2ff93618.jpg", "Url" =>"http://apix.sinaapp.com/cet/index.php?openid=".$object->FromUserName);
68                 break;
69         }
70         if(is_array($content)){
71             $result = $this->transmitNews($object, $content);
72         }else{
73             $result = $this->transmitText($object, $content);
74         }
75         return $result;
76     }
77
78     private function receiveText($object)
79     {
80         $keyword = trim($object->Content);
81         if (strstr($keyword, "四六级") || strstr($keyword, "英语")){
82             $content = array();
83             $content[] = array("Title" =>"2014年6月全国大学英语四六级考试成绩查询","Description" =>"", "PicUrl" =>"http://365jia.cn/uploads/13/0301/5130c2ff93618.jpg", "Url" =>"http://apix.sinaapp.com/cet/index.php?openid=".$object->FromUserName);
84         }else{
85             $content = date("Y-m-d H:i:s",time())."\n技术支持 方倍工作室";
86         }
87         if(is_array($content)){
88             $result = $this->transmitNews($object, $content);
89         }else{
90             $result = $this->transmitText($object, $content);
91         }
92         return $result;
93     }
94
95     private function transmitText($object, $content)
96     {
97         $textTpl = "<xml>
98 <ToUserName><![CDATA[%s]]></ToUserName>
99 <FromUserName><![CDATA[%s]]></FromUserName>
100 <CreateTime>%s</CreateTime>
101 <MsgType><![CDATA[text]]></MsgType>
102 <Content><![CDATA[%s]]></Content>
103 </xml>";
104         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
105         return $result;
106     }
107
108     private function transmitNews($object, $arr_item)
109     {
110         if(!is_array($arr_item))
111             return;
112
113         $itemTpl = "    <item>
114         <Title><![CDATA[%s]]></Title>
115         <Description><![CDATA[%s]]></Description>
116         <PicUrl><![CDATA[%s]]></PicUrl>
117         <Url><![CDATA[%s]]></Url>
118     </item>
119 ";
120         $item_str = "";
121         foreach ($arr_item as $item)
122             $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
123
124         $newsTpl = "<xml>
125 <ToUserName><![CDATA[%s]]></ToUserName>
126 <FromUserName><![CDATA[%s]]></FromUserName>
127 <CreateTime>%s</CreateTime>
128 <MsgType><![CDATA[news]]></MsgType>
129 <Content><![CDATA[]]></Content>
130 <ArticleCount>%s</ArticleCount>
131 <Articles>
132 $item_str</Articles>
133 </xml>";
134
135         $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
136         return $result;
137     }
138
139     private function logger($log_content)
140     {
141     }
142 }
143
144
145 ?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: