微信相应代码的实现 分类: php 微信开发 2015-01-29 23:26 25人阅读 评论(0) 收藏
<?php // http://tstcwx.sinaapp.com/sea_server.php /* 微信帐号echo_server的实现 */ /*定义TOKEN为 token*/ define("TOKEN","token"); include 'jwgl.class.php'; // 登录教务系统的类的文件 //函数功能:验证微信的来源的正确性。 function checkSignature() { // 获取GET参数 $signature = $_GET['signature']; $nonce = $_GET['nonce']; $timestamp = $_GET['timestamp']; //把nonce timestamp和token组装到数列里并排序 $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); //把数组中的元素合并成字符串 $tmpStr = implode($tmpArr); //shal加密 $tmpStr = sha1($tmpStr); if($tmpStr == $signature) { //相等就返回echostr return true; } else { return false; } } //判断 if(false == checkSignature()) { exit(0); } // 获取echostr $echostr = $_GET['echostr']; if($echostr) { echo $echostr; exit(0); } // 分析用户输入并输出 //获取POST数据 function getPostData() { global $HTTP_RAW_POST_DATA; $data = $HTTP_RAW_POST_DATA; // 判断post数据是否为空 if(!$data) { echo "wrong input!"; exit(0); } return $data; } $PostData = getPostData(); // 解析XML字符串 $xmlObj = simplexml_load_string($PostData,'SimpleXMLElement',LIBXML_NOCDATA); if(!$xmlObj) { echo 'wring input!'; exit(0); } //获取FromUserName $fromUserName = $xmlObj->FromUserName; //获取ToUserName $toUserName = $xmlObj->ToUserName; //获取MsgType $msgType = $xmlObj->MsgType; //函数功能 检查数据库中是否有该用户数据,若存在则返回账户密码,若不存在则返回false function checkopenid() { $db_host=SAE_MYSQL_HOST_M; //连接的服务器地址 $db_user=SAE_MYSQL_USER; //连接数据库的用户名 $db_psw=SAE_MYSQL_PASS; //连接数据库的密码 $db_name=SAE_MYSQL_DB; //连接的数据库名称 global $fromUserName; $db = new MySQLi($db_host,$db_user,$db_psw,$db_name,SAE_MYSQL_PORT); if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit; } $query = "select * from user where openid = '".$fromUserName."'"; $result = $db->query($query); $num_results = $result->num_rows; sae_debug($num_results); if($num_results != 0) { $row = $result->fetch_array(); //sae_debug(error_msg(str_split($row))); //var_dump($row); sae_debug(count($row)); $stu = array('stuID' => $row['SID'],'SIDpasswd' => $row['SIDpasswd']); //echo '<br>'; //var_dump($stu); //echo '<br>enter'; return $stu; } else { return NULL; } //echo '<br>'.$num_results.'<br>'; } if('text' != $msgType) { $retMsg = '只支持文本消息'; } else { if(($stu = checkopenid()) != NULL) { // 若数据库中有数据 //sae_debug(count($stu)); $stuID = $stu['stuID']; $stuIDpasswd = $stu['SIDpasswd']; sae_debug($stuID); sae_debug($stuIDpasswd); //sae_debug('stuid'); $jw = new jwgl(); $jw->setuse($stuID,$stuIDpasswd); if($jw->login() == false) { sae_debug('login error'); //Header("Location: loginform.html"); } else $results = $jw->get_grade(); //保存查询结果 $count = count($results); sae_debug($count); foreach($results as $values) { //var_dump($values); //echo '<br>-------------------------------------------<br>'; //$temp = $temp. "学年:".$values['学年']." 学期:".$values["学期"]." 课程名称:".$values['课程名称']." 成绩:".$values['成绩']; $temp = $temp." 课程名称:".$values['课程名称']." 成绩:".$values['成绩']; } sae_debug($temp); $retMsg = $temp; } else { //若数据库中没有数据 $retMsg = 'http://tstcwx.sinaapp.com/login.php?openid='.$fromUserName; } } //输出消息的XML模版 $retTmp = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $resultStr = sprintf($retTmp,$fromUserName,$toUserName,time(),$retMsg); //输出XML描述的消息 echo $resultStr;
版权声明:本文为博主原创文章,未经博主允许不得转载。