<?php
/**
* wechat php test
*/
//define your token
include_once("../php/common.inc");
include_once("../php/function.php");
define("TOKEN", "nbntzs");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
//echo $echoStr;
$this->responseMsg();
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
global $db_server,$db_login,$db_password,$db;
$conn = mysql_connect($db_server,$db_login,$db_password);
mysql_select_db($db, $conn);
$qry = mysql_query("SET NAMES 'utf8'") or startUpError('.SET NAMES Error.');
$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);
$RX_TYPE = trim($postObj->MsgType);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$textgz="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></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>";
$textnews_top="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>";
$textnews_con="<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$textnews_bot="</Articles>
</xml>";
if($RX_TYPE=="event"){
if ($postObj->Event=="subscribe"){
$zhf_sql="select * from wx where wx_lx='1' and wx_publish='0' order by tuijian_paixu asc,wx_update asc limit 0,10";
$result_show=mysql_query($zhf_sql,$conn);
$result_top = sprintf($textnews_top, $fromUsername, $toUsername, $time,mysql_num_rows($result_show));
while ($obj_show=mysql_fetch_assoc($result_show)){
$result_con.=sprintf($textnews_con,$obj_show["wx_title"],$obj_show["wx_con"],"/images/pic/lb/".$obj_show["wx_pic"],$obj_show["wx_link"]);
}
echo $result_top.$result_con.$textnews_bot;
}
} else {
$zhf_sql="select * from wx where wx_lx!='1' and wx_publish='0' and FIND_IN_SET('".$keyword."',wx_key) and wx_hfid='0' order by tuijian_paixu asc,wx_update asc";
$result_show=mysql_query($zhf_sql,$conn);
if (mysql_num_rows($result_show)=="0"){
$zhf_sql="select * from wx where wx_lx!='1' and wx_publish='0' and wx_hfid='0' and wx_key='默认回复' order by tuijian_paixu asc,wx_update asc";
$result_show=mysql_query($zhf_sql,$conn);
}
if ($obj_show=mysql_fetch_assoc($result_show)){
switch ($obj_show["wx_lx"]){
case "3":
$result_con_show=sprintf($textnews_con,str_replace("<BR>","\n",$obj_show["wx_title"]),str_replace("<BR>","\n",$obj_show["wx_con"]),"/images/pic/lb/".$obj_show["wx_pic"],$obj_show["wx_link"]);
$result_top = sprintf($textnews_top, $fromUsername, $toUsername, $time,"1");
echo $result_top.$result_con_show.$textnews_bot;
break;
case "4":
$result_con_show=sprintf($textnews_con,$obj_show["wx_title"],$obj_show["wx_con"],"images/pic/lb/".$obj_show["wx_pic"],$obj_show["wx_link"]);
$hf_sql="select * from wx where wx_lx!='1' and wx_publish='0' and wx_hfid='".$obj_show["wx_id"]."' order by tuijian_paixu asc,wx_update asc limit 0,10";
$result_hf=mysql_query($hf_sql,$conn);
$hf_sl=mysql_num_rows($result_hf)+1;
$result_top = sprintf($textnews_top, $fromUsername, $toUsername, $time,$hf_sl);
while ($obj_hf=mysql_fetch_assoc($result_hf)){
$result_con_show.=sprintf($textnews_con,str_replace("<BR>","\n",$obj_hf["wx_title"]),$obj_hf["wx_con"],"/images/pic/lb/".$obj_hf["wx_pic"],$obj_hf["wx_link"]);
}
echo $result_top.$result_con_show.$textnews_bot;
break;
case "2":
$resultStr4 = sprintf($textTpl, $fromUsername, $toUsername, $time, str_replace("<BR>","\n",$obj_show["wx_con"]));
echo $resultStr4;
break;
}
}
//echo $result_top.$result_con.$textnews_bot;
}
}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;
}
}
}
?>
此功能包括:关注自动回复,文本回复,单图片回复,多图片回复,关键词默认回复
|