<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$dbname = '';
$host = '';
$port = '';
$user = '';
$pwd = '';
/*接着调用mysql_connect()连接服务器*/
$link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);
if(!$link) {
die("Connect Server Failed: " . mysql_error($link));
}
/*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
if(!mysql_select_db($dbname,$link)) {
die("Select Database Failed: " . mysql_error($link));
}
/*至此连接已完全建立,就可对当前数据库进行相应的操作了*/
/*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/
/* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/
/**
* 接下来就可以使用其它标准php mysql函数操作进行数据库操作
*/
if(strlen($keyword)<14)
{
$contentStr ="考生号错误";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
else
{
$k1=substr($keyword,0,14);
$k2=substr($keyword,14);
$k3="";
mysql_query("set names UTF8",$link);
$sql="SELECT `xm`,`lqzy` FROM `hyq_student` WHERE ksh=".$k1;
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
if(strlen($k2)>0)
$k3=",且你的反馈信息(".$k2.")已被记录";
if($rs)
$contentStr = $rs['xm']."同学你好!你已被贵州民族大学人文科技学院".$rs['lqzy']."专业预录取".$k3."!0851-83606673";
else
$contentStr ="未查询到该考生号学生信息!0851-83606673";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
$sqlt="UPDATE `hyq_student` SET `jd`='".$k2."',`cx`='微信查询' WHERE ksh=".$k1;
mysql_query($sqlt);
mysql_close($link);
}
}
}else{
echo "";
exit;
}
}
}
?>
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$dbname = '';
$host = '';
$port = '';
$user = '';
$pwd = '';
/*接着调用mysql_connect()连接服务器*/
$link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);
if(!$link) {
die("Connect Server Failed: " . mysql_error($link));
}
/*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
if(!mysql_select_db($dbname,$link)) {
die("Select Database Failed: " . mysql_error($link));
}
/*至此连接已完全建立,就可对当前数据库进行相应的操作了*/
/*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/
/* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/
/**
* 接下来就可以使用其它标准php mysql函数操作进行数据库操作
*/
if(strlen($keyword)<14)
{
$contentStr ="考生号错误";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
else
{
$k1=substr($keyword,0,14);
$k2=substr($keyword,14);
$k3="";
mysql_query("set names UTF8",$link);
$sql="SELECT `xm`,`lqzy` FROM `hyq_student` WHERE ksh=".$k1;
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
if(strlen($k2)>0)
$k3=",且你的反馈信息(".$k2.")已被记录";
if($rs)
$contentStr = $rs['xm']."同学你好!你已被贵州民族大学人文科技学院".$rs['lqzy']."专业预录取".$k3."!0851-83606673";
else
$contentStr ="未查询到该考生号学生信息!0851-83606673";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
$sqlt="UPDATE `hyq_student` SET `jd`='".$k2."',`cx`='微信查询' WHERE ksh=".$k1;
mysql_query($sqlt);
mysql_close($link);
}
}
}else{
echo "";
exit;
}
}
}
?>