[PHP] 纯文本查看 复制代码
define("TOK", "*");
$wcObj = new wcCallapiTest();
if (isset($_GET['echostr']))
{
$wcObj->valid();
}else
{
$wcObj->resMsg();
}
class wcCallapiTest{
public function valid(){
$echoStr = $_GET["echostr"];
if($this->checkSig()){
echo $echoStr;
exit;
}
}
private function checkSig(){
$sig = $_GET["sig"];
$time = $_GET["time"];
$nonce = $_GET["nonce"];
$token = TOK;
$tmpArr = array($token, $time, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $sig ){
return true;
}else{
return false;
}
}
public function resMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$time = time();
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$Username = $postObj->UserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
if($postObj->Event=="subscribe"){
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<UserName><![CDATA[%s]]></UserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$msgType="text";
$content="已关注";
$resultStr = sprintf($textTpl, $Username, $toUsername, time(),$msgType, $content,$flag);
echo $resultStr;
}elseif($keyword){
if (strstr($keyword,"您好") || strstr($keyword,"您好") || strstr($keyword,"是否在线") || strstr($keyword,"在线") || strstr($keyword,"Hi") || strstr($keyword,"无人在") || strstr($keyword,"无人在线") || strstr($keyword,"?")){
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<UserName><![CDATA[%s]]></UserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$content='有什么问题请提问';
$msgType="text";
echo $resultStr = sprintf($textTpl, $Username, $toUsername, time(),$msgType,$content);
}elseif (substr($keyword,-6)=='天气'){
$items=1;
$data=crawl_weather($keyword);
$picTplhead = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<UserName><![CDATA[%s]]></UserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount><![CDATA[%s]]></ArticleCount>
<Articles>";
$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>";
$resultStrhead = sprintf($picTplhead, $Username, $toUsername, time(), 4);
$i=1;
if (count($data)){
foreach($data as $f){
$title=$f["date"]."\r\n".$f["temp"].' '.$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>
<UserName><![CDATA[%s]]></UserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$content="搜不到[".str_replace("天气","",$keyword).']天气!';
$resultStr = sprintf($textTpl, $Username, $toUsername, time(),$content,$flag);
echo $resultStr;
}
}else{
$xml=crawlContent($keyword,"search");
$xmlArr=xml_to_array($xml);
$items=count($xmlArr["pre"]);
if ($items){
$picTplhead = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<UserName><![CDATA[%s]]></UserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount><![CDATA[%s]]></ArticleCount>
<Articles>";
$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>";
$tickets_count=$items>=7?1:$items;
$resultStrhead = sprintf($picTplhead, $Username, $toUsername, time(), $tickets_count+1);
if ($items>=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){
foreach($xmlArr as $k=>$f){
if (strlen($f["url"])>0){
$resultStrhead = sprintf($picTplhead, $Username, $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://test.test/test.png[/img]';
$url='http://test.test/search?q='.$keyword;
$resultpictpl .= sprintf($picTpl,$title,$description,$image,$url);
$resultpictplfoot = sprintf($picTplfoot);
echo $resultStr = $resultStrhead.$resultpictpl.$resultpictplfoot;
}
}
}
}else{
echo "已关注";
exit;
}
}
}
function crawlContent($keyword,$act){
$url="http://test.test/search?q=?key=$keyword";
//如下
/*
$html.='<?xml version="1.0" encoding="UTF-8"?><route>';
for ($i=0;$i<=8;$i++){
$url='<url>URL</url>';
$picurl='<picurl>URL</picurl>';
$title='<title>Head</title>';
$desc='<desc>Description</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/6.0 (Windows NT 6.2) AppleWebKit/547.12 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/538.12');
$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='http://test.test/weather?location='.str_replace("天气","",$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"];
}