[PHP] 纯文本查看 复制代码
define("TOKEN", "****");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])){
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest{
public function valid(){
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
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;
}
}
public function responseMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$time = time();
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
if($postObj->Event=="subscribe"){//判断是否是新关注,如果是执行下面语句
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$msgType="text";
$content="亲,感谢关注";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(),$msgType, $content,$flag);
echo $resultStr;
}elseif($keyword){//获取用户发送的关键词取得相关内容
if (strstr($keyword,"你好") || strstr($keyword,"您好") || strstr($keyword,"在吗") || strstr($keyword,"在不") || strstr($keyword,"喂") || strstr($keyword,"有人没") || strstr($keyword,"有人吗") || strstr($keyword,"?")){
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$content='亲,想要什么直接回复想要的词就可以哦!';
$msgType="text";
echo $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(),$msgType,$content);
}elseif (substr($keyword,-6)=='天气'){//获取天气
$items=1;
$data=crawl_weather($keyword);
$picTplhead = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount><![CDATA[%s]]></ArticleCount>
<Articles>";//与下面的picTpl拼接
$picTpl = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$picTplfoot = "</Articles>
<FuncFlag>0</FuncFlag>
</xml>";//与上面的picTpl拼接
$resultStrhead = sprintf($picTplhead, $fromUsername, $toUsername, time(), 4);//图文消息头部
$i=1;
if (count($data)){
foreach($data as $f){
$title=$f["date"]."\r\n".$f["temperature"].' '.$f["weather"].' '.$f["wind"];
$description=$f["Weather"].$f["wind"];
$imageurl=($i==1?'':$f["dayPictureUrl"]);
$resultpictpl .= sprintf($picTpl,$title,$description,$imageurl,'');
$i++;
}
$resultpictplfoot = sprintf($picTplfoot);//图文消息尾部
echo $resultStr = $resultStrhead.$resultpictpl.$resultpictplfoot;
}else{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$content="没有查询到【".str_replace("天气","",$keyword).'】的天气!';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(),$content,$flag);
echo $resultStr;
}
}else{//以上两个条件都不满足,则执行以下语句,根据关键词传到crawlContent
$xml=crawlContent($keyword,"search");//抓取数据库
$xmlArr=xml_to_array($xml);//解析xml为数组
$items=count($xmlArr["pre"]);
if ($items){
$picTplhead = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount><![CDATA[%s]]></ArticleCount>
<Articles>";//与下面的picTpl拼接
$picTpl = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$picTplfoot = "</Articles>
<FuncFlag>0</FuncFlag>
</xml>";//与上面的picTpl拼接
$tickets_count=$items>=7?1:$items;
$resultStrhead = sprintf($picTplhead, $fromUsername, $toUsername, time(), $tickets_count+1);//图文消息头部
if ($items>=7){//如果获取到的内容大于7条
foreach($xmlArr as $k=>$f){
$title=$f["title"];
$description=$f["desc"];
$image=$f["picurl"];
$url=$f["url"];
$resultpictpl .= sprintf($picTpl,$title,$description,$image,$url);
}
}elseif($items<7 && $items>0){//如果获取到的内容小于7条and大于0条
foreach($xmlArr as $k=>$f){
if (strlen($f["url"])>0){//如果只有一条记录执行
$resultStrhead = sprintf($picTplhead, $fromUsername, $toUsername, time(), 2);//图文消息头部,如果只有一条记录
$title='【'.$f["type"].'】'.$f["title"];
$description=$f["desc"];
$image=$f["picurl"];
$url=$f["url"];
$resultpictpl .= sprintf($picTpl,$title,$description,$image,$url);
}else{
foreach($f as $k=>$list){
$title='【'.$list["type"].'】'.$list["title"];
$description=$list["desc"];
$image=$list["picurl"];
$url=$list["url"];
$resultpictpl .= sprintf($picTpl,$title,$description,$image,$url);
}
}
}
}
//附加最后一条内容
$title="没有找到满意的内容?\r\n戳这里查看更多【".$keyword."】内容!";
$description='';
$image='[img]http://www.52pojie.cn/static/image/common/logo.png[/img]';//请改为自己的默认图片
$url='[url=http://so.52pojie.cn/cse/search?q=]http://so.52pojie.cn/cse/search?q='.[/url]$keyword;//请改为自己需要的网址
$resultpictpl .= sprintf($picTpl,$title,$description,$image,$url);
$resultpictplfoot = sprintf($picTplfoot);//图文消息尾部
echo $resultStr = $resultStrhead.$resultpictpl.$resultpictplfoot;
}
}
}
}else{
echo "亲,感谢关注";
exit;
}
}
}
function crawlContent($keyword,$act){//根据关键词抓取线路
$url="[url=http://so.52pojie.cn/cse/search?q=?key=]http://so.52pojie.cn/cse/search?q=?key=[/url]$keyword";
//此url是将抓取的url,请自行写代码,这里不写代码了大致思路就是根据key查询相关内容,输入xml格式。
//如下
/*
$html.='<?xml version="1.0" encoding="UTF-8"?><route>';
for ($i=0;$i<=8;$i++){
$url='<url>网址</url>';
$picurl='<picurl>图片地址</picurl>';
$title='<title>标题</title>';
$desc='<desc>描述</desc>';
$html.='<pre>';
$html.=$url.$picurl.$title.$desc;
$html.='</pre>';
}
$html.='</route>';
echo $html;*/
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch,CURLOPT_NOBODY,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,0);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11');
$html=curl_exec($ch);
curl_close($ch);
return $html;
}
function xml_to_array($xml){
$array = (array)(simplexml_load_string($xml));
foreach ($array as $key=>$item){
$array[$key] = struct_to_array((array)$item);
}
return $array;
}
function struct_to_array($item){
if(!is_string($item)) {
$item = (array)$item;
foreach ($item as $key=>$val){
$item[$key] = struct_to_array($val);
}
}
return $item;
}
function crawl_weather($keyword){
$ch=curl_init();
$headurl='[url=http://api.map.baidu.com/telematics/v3/weather?location=]http://api.map.baidu.com/telematics/v3/weather?location='.str_replace[/url]("天气","",$keyword).'&output=json&ak=这里是自己的ak';
curl_setopt($ch,CURLOPT_URL,$headurl);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch);
curl_close($ch);
$data=json_decode($data,true);
return $data["results"][0]["weather_data"];
}