$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 ))
{
//用 戶 名 : $user
//密 碼 : $pwd
//主庫域名 : $host
//從庫域名 : SAE_MYSQL_HOST_S
//端 口 : $port
//數(shù)據(jù)庫名 : $dbname
$dbname = "app_dq095";
$host = "w.rdc.sae.sina.com.cn";
$port = "3306";
$user = "4k514n103z";
$pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";
/*接著調(diào)用mysql_connect()連接服務(wù)器*/
// 連主庫
$db = mysql_connect($host,$user,$pwd);
if(!$db){
die("Connect Server Failed: " . mysql_error($db));
}
/*連接成功后立即調(diào)用mysql_select_db()選中需要連接的數(shù)據(jù)庫*/
if (!mysql_select_db($dbname)) {
die("Select Database Failed: " . mysql_error($db));
}
mysql_query("set names utf-8",$db);
/*至此連接已完全建立,就可對(duì)當(dāng)前數(shù)據(jù)庫進(jìn)行相應(yīng)的操作了*/
/*?。?!注意,無法再通過本次連接調(diào)用mysql_select_db來切換到其它數(shù)據(jù)庫了?。。?/
/* 需要再連接其它數(shù)據(jù)庫,請(qǐng)?jiān)偈褂胢ysql_connect+mysql_select_db啟動(dòng)另一個(gè)連接*/
/**
* 接下來就可以使用其它標(biāo)準(zhǔn)php mysql函數(shù)操作進(jìn)行數(shù)據(jù)庫操作
*/
$sql="SELECT * FROM `welcome`WHERE `user`= '" . iconv("UTF-8","GBK",$fromUsername) . "'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$b= $rs['user'];
$c=iconv("GBK","UTF-8",$b);
$msgType = "text";
if ($c==$fromUsername)
{
$contentStr = "歡迎老朋友!";
}else{
$sql="INSERT INTO `welcome`(`id`,`user`) VALUES (NULL,'{$fromUsername}')";
mysql_query($sql);
$contentStr = "歡迎新朋友!";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
mysql_close($db);
}else{
echo "Input something...";
}
?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
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;
$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 ))
{
//用 戶 名 : $user
//密 碼 : $pwd
//主庫域名 : $host
//從庫域名 : SAE_MYSQL_HOST_S
//端 口 : $port
//數(shù)據(jù)庫名 : $dbname
$dbname = "app_dq095";
$host = "w.rdc.sae.sina.com.cn";
$port = "3306";
$user = "4k514n103z";
$pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";
/*接著調(diào)用mysql_connect()連接服務(wù)器*/
// 連主庫
$db = mysql_connect($host,$user,$pwd);
if(!$db){
die("Connect Server Failed: " . mysql_error($db));
}
/*連接成功后立即調(diào)用mysql_select_db()選中需要連接的數(shù)據(jù)庫*/
if (!mysql_select_db($dbname)) {
die("Select Database Failed: " . mysql_error($db));
}
mysql_query("set names utf-8",$db);
/*至此連接已完全建立,就可對(duì)當(dāng)前數(shù)據(jù)庫進(jìn)行相應(yīng)的操作了*/
/*!??!注意,無法再通過本次連接調(diào)用mysql_select_db來切換到其它數(shù)據(jù)庫了?。?!*/
/* 需要再連接其它數(shù)據(jù)庫,請(qǐng)?jiān)偈褂胢ysql_connect+mysql_select_db啟動(dòng)另一個(gè)連接*/
/**
* 接下來就可以使用其它標(biāo)準(zhǔn)php mysql函數(shù)操作進(jìn)行數(shù)據(jù)庫操作
*/
$sql="SELECT * FROM `welcome`WHERE `user`= '" . iconv("UTF-8","GBK",$fromUsername) . "'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$b= $rs['user'];
$c=iconv("GBK","UTF-8",$b);
$msgType = "text";
if ($c==$fromUsername)
{
$contentStr = "歡迎老朋友!";
}else{
$sql="INSERT INTO `welcome`(`id`,`user`) VALUES (NULL,'{$fromUsername}')";
mysql_query($sql);
$contentStr = "歡迎新朋友!";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
mysql_close($db);
}else{
echo "Input something...";
}
}else {
echo "";
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;
}
}
}
?>