<?php
//连接access
//$mdb ="\ecbi_db\ y0919fj_36_ec.mdb";
$mdb ="/ecbi_db/y0919fj_36_ec.mdb";
//$mdb="y0919fj_36_ec.mdb";//access数据文件的相对路径,默认和本文件同级
$connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath($mdb);
$conn = odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );//关于函数的引用
if($conn)
{
//获取微信发送数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
//解析数据
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$usertime=$postObj->CreateTime;
$keyword = trim($postObj->Content);
//将用户的关键字转化为GBK格式,以对数据库操作
$usertime=mb_convert_encoding($usertime,"GBK","UTF-8");
$keyword =mb_convert_encoding($keyword,"GBK","UTF-8");
//将用户的数据保存到数据库
$name=mb_convert_encoding($fromUsername,"GBK","UTF-8");
$dd=odbc_exec($conn,"insert into KS_UserWrite (UserID,UserContent) values ('$name','$keyword')");
//xml模板
$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>";
$newsTpl1="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>";
$newsTpl2="<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$newsTpl3="</Articles>
<FuncFlag>0</FuncFlag>
</xml> ";
//用关键字查询数据库,bug:限制条数,不是固定取出多少条
$cc=odbc_exec($conn,"SELECT top 5 * FROM KS_Article where ArticleContent like '%".$keyword."%'and PicNews=1");
if(odbc_fetch_array($cc))//bug%%%%%%%%%%%%%%%
{
$time=time();
$msgType = "news";
$articlecount=4;
$resultStr1=sprintf($newsTpl1,$fromUsername,$toUsername,$time,$msgType,$articlecount);
while ($cd=odbc_fetch_array($cc))
{
$title=mb_convert_encoding($cd["Title"],"UTF-8","GBK");
$description=mb_convert_encoding($cd["Intro"],"UTF-8","GBK");
$picurl=mb_convert_encoding($cd["PhotoUrl"],"UTF-8","GBK");
$url=mb_convert_encoding($cd["ID"],"UTF-8","GBK");
$resultStr2 = $resultStr2.sprintf($newsTpl2,$title,$description,"http://fjemoney.cn".$picurl,"http://www.fjemoney.cn/Item/Show.asp?m=1&d=".$url);
}
echo $resultStr1.$resultStr2.$newsTpl3;
}
else
{
$time=time();
$msgType="text";
$contentStr="您输入的数据我们暂时还未录入!";
$resultStr= sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo $resultStr;
}
}
else
{
echo "未接收到您的数据!";
exit;
}
}
else
{
echo "连接数据库失败";
}
?>
微信公众平台开发——企业网站与微信的信息推送
最新推荐文章于 2025-08-13 17:42:51 发布