微信公眾號開發之(39)翻頁查詢
阿新 • • 發佈:2019-02-08
<?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;
$type = $postObj->MsgType;
$customevent = $postObj ->Event;
$latitude = $postObj->Location_X;
$longitude = $postObj->Location_Y;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content>%s</Content>
<FuncFlag>0</FuncFlag>
</xml>";
switch ($type)
{
case "event";
if ($customevent=="subscribe")
{$contentStr = "感謝你的關注\n欄目正在搭建,敬請期待\n回覆1看視訊教程";}
break;
case "image";
$contentStr = "你的圖片很棒!";
break;
case "location";
$contentStr = "你的緯度是{$latitude},經度是{$longitude},我已經鎖定!";
break;
case "link" ;
$contentStr = "你的連結有病毒吧!";
break;
case "text";
include("coon.php");
$num = "SELECT * FROM `kecheng` ";
$que=mysql_query($num);
$no=mysql_num_rows($que);//獲得條數
$sumpage=ceil($no/7);
$page=(intval($keyword)-1)*7;
$total=$no-$page+1;
if($total>8)
{$total=8;}
$sql = "SELECT * FROM `kecheng` ORDER BY `id` DESC LIMIT {$page},7";
$query=mysql_query($sql);
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>$total</ArticleCount>
<Articles>
<item>
<Title><![CDATA[總共{$sumpage}頁,輸入頁數翻頁]]></Title>
<Description><![CDATA[]]></Description>
<PicUrl>http://autoguitar.duapp.com/1.jpg</PicUrl>
<Url><![CDATA[]]></Url>
</item>";
while($rs=mysql_fetch_array($query)){
$newsTpl.="<item>
<Title>$rs[content]</Title>
<Description><![CDATA[]]></Description>
<PicUrl>http://dq095.applinzi.com/2.jpg</PicUrl>
<Url><![CDATA[]]></Url>
</item>";
}
$newsTpl.="</Articles>
<FuncFlag>0</FuncFlag>
</xml>";
$myresultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time);
echo $myresultStr;
break;
default;
$contentStr ="此項功能尚未開發";
}
$msgType="text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}
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;
}
}
}
?>