新闻中心
微信开发很简单,官网也提供DEOM文档,但难在如何从数据库读取整合到文档中,也就是数据拼接。本代码是我司开发中实现原理分享出来的,如果你正在开发那么一起来学习吧。
实现功能:当用户输入聊天内容给公众号时根不同词从数据库读取新闻或产品以图文形式返回给用户。当然如果你也是网站建设从业者可以购买我司的建站系统平台版,可二次开发。 详情登陆:http://www.yinxi.net/design/#websites-taocan 1880元起(单用户),多用户版(8000元,自助建站平台)
<?php define("TOKEN", "dy_website");//与管理平台的TOKEN设置一致 $wechatObj = new wechatCallbackapiTest(); //$wechatObj->valid(); if(isset($_GET["echostr"])){ $wechatObj->valid(); } else{ session_start(); require_once("../common/init.php"); $web=get_web(); $wechatObj->responseMsg(); } class wechatCallbackapiTest { public function valid()//验证接口用,管理平台后台设置的时候请调用此方法进行验证 { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg()//接受用户信息并返回图文信息 { $postStr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input"); if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $msgType=$postObj->MsgType; $latitude=$postObj->Location_x; $longitude=$postObj->Location_y; $event=$postObj->Event; $eventKey=$postObj->EventKey; $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>"; //加载图文模版 $picTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>%s</ArticleCount> %s <FuncFlag>1</FuncFlag> </xml> "; if($msgType == "event" and $Event == "subscribe")//判断是否是新关注 { $msgType = "text"; $contentStr = "您好,欢迎您关注巅云智能建站系统PHP开发网"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }elseif($msgType == "event" and $event=="CLICK" or !empty($keyword))//用户输入的内容 { $msgType = "news"; session_start(); require_once("../common/init.php"); $web=get_web(); $con=mysql_connect(DB_SERVER,DB_LOGIN,DB_PW) or die("Unable to connect to the MySQL!"); $db = mysql_select_db(DB_NAME,$con); $sqlF = "select * from products where show_index=0 and Type='product' and user='$web' and (ProductName like '%$keyword%' or txtDetail like '%$keyword%' or ProductIntro like '%$keyword%' or Price like '%$keyword%') "; $rsF = mysql_query($sqlF); $countF = mysql_num_rows($rsF); //产品 if(strpos($keyword, "产品") !== false or $countF>=1 ){ if(strpos($keyword, "产品") !== false){ $wheres=""; } else{ $wheres="and (ProductName like '%$keyword%' or txtDetail like '%$keyword%' or ProductIntro like '%$keyword%' or Price like '%$keyword%')"; } $sqlc = "select * from products where show_index=0 and Type='product' and user='$web' $wheres"; $rsc = mysql_query($sqlc); $countpro = mysql_num_rows($rsc); if($countpro>8) { $countpro=7; } $sql = "select * from products where show_index=0 and Type='product' and user='$web' $wheres order by rand() limit 40 "; $rs = mysql_query($sql); $strpro='<Articles>'; while($row_p=mysql_fetch_assoc($rs)){ $Content1=mb_substr(strip_tags(str_replace(' ','',str_replace('"','', $row_p['txtDetail']))),0,25,'utf-8').""; $row_p['ProductName'] = mb_substr($row_p['ProductName'],0,12,'utf-8').""; $Contentall1=$row_p['txtDetail']; $PInfo=PInfo($row_p['IDProduct']); if($PInfo['iPrice']){ $PInfo['iPrice']=" ¥".$PInfo['iPrice']; }else{ $PInfo['iPrice']=$PInfo['iPrice']; } $query1 ="select pro_open_pageid from notes where showIndex=1 and pro_open_pageid !='' and note_class='product' and user='$web' and mobile='1' "; $rscp = mysql_query($query1); $rscpageid = mysql_fetch_row($rscp); $page_open=$rscpageid[0]; $httpurl=$httpurl1="http://".$_SERVER['HTTP_HOST']; $pics = json_decode($row_p['pics'], true); if (isset($pics)) { foreach ($pics as $key=>$v) { $picinfo[$key]= $picinfo[$key]; $row_p['pic']=$pics[0]; } } $src1 = $row_p['pic']; if($src1==""){ $src1 = "/admin/images/dingdan.png"; } if(stripos($src1,'http://') !== false) { $httpurl=""; } $strpro.="<item> <Title><![CDATA[{$row_p['ProductName']}{$PInfo['iPrice']} ]]></Title> <Description><![CDATA[{$Content1}]]></Description> <PicUrl><![CDATA[{$httpurl}/{$src1}]]></PicUrl> <Url><![CDATA[{$httpurl1}/mobile/index.php?proID={$row_p['IDProduct']}&page_id={$page_open}]]></Url> </item>"; } $strpro.='</Articles>'; $resultStr = sprintf($picTpl, $fromUsername, $toUsername, strtotime($row_p['dtCreate']), $msgType,$countpro, $strpro); if($countpro==0){ $msgType = "text"; $contentStr = "没找到任何相关内容产品!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } }//产品over else { $sqlc = "select * from news where showIndex=1 and user='$web' and (NewsID like '%$keyword%' or Title like '%$keyword%' or Content like '%$keyword%') "; $rsc = mysql_query($sqlc); $countnews = mysql_num_rows($rsc); if($countnews>8) { $countnews=7; } $sql = "select * from news where showIndex=1 and user='$web' and (NewsID like '%$keyword%' or Title like '%$keyword%' or Content like '%$keyword%') limit 40 "; $rs = mysql_query($sql); $strnews='<Articles>'; while($row_p=mysql_fetch_assoc($rs)){ $Content1=mb_substr(strip_tags(str_replace(' ','',str_replace('"','', $row_p['Content']))),0,25,'utf-8').""; $row_p['Title'] = mb_substr($row_p['Title'],0,12,'utf-8').""; $Contentall1=$row_p['Content']; preg_match_all('/<img[^>]*src\s?=\s?[\'|"]([^\'|"]*)[\'|"]/is', $Contentall1, $picarr); $query1 ="select news_open_pageid from notes where showIndex=1 and news_open_pageid !='' and note_class='news' and user='$web' and mobile='1' "; $rscp = mysql_query($query1); $rscpageid = mysql_fetch_row($rscp); $page_open=$rscpageid[0]; $httpurl=$httpurl1="http://".$_SERVER['HTTP_HOST']; $src1 = $picarr[1][0]; if(stripos($src1,'http://') !== false) { $httpurl=""; } if($src1!=""){ $strnews.="<item> <Title><![CDATA[{$row_p['Title']}]]></Title> <Description><![CDATA[{$Content1}]]></Description> <PicUrl><![CDATA[{$httpurl}/{$src1}]]></PicUrl> <Url><![CDATA[{$httpurl1}/mobile/index.php?NewsID={$row_p['NewsID']}&page_id={$page_open}]]></Url> </item>"; } } $strnews.='</Articles>'; $resultStr = sprintf($picTpl, $fromUsername, $toUsername, strtotime($row_p['dtCreate']), $msgType,$countnews, $strnews); if($countnews==0){ $msgType = "text"; $contentStr = "没找到任何相关内容!!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } } echo $resultStr; }else{ echo "说点什么吧!"; } }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; } } } ?>