申请会员ID:遭遇花无缺【申请通过】
之前的标题好像错了,在这里改过,望管理员谅解。1、申 请 I D:遭遇花无缺
2、个人邮箱:52023374@qq.com
3、原创技术文章:
微信公众号开发,相关功能见注释。
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><!]></ToUserName>
<FromUserName><!]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><!]></MsgType>
<Content><!]></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><!]></ToUserName>
<FromUserName><!]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><!]></MsgType>
<Content><!]></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><!]></ToUserName>
<FromUserName><!]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><!]></MsgType>
<ArticleCount><!]></ArticleCount>
<Articles>";//与下面的picTpl拼接
$picTpl = "<item>
<Title><!]></Title>
<Description><!]></Description>
<PicUrl><!]></PicUrl>
<Url><!]></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><!]></ToUserName>
<FromUserName><!]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><!]></MsgType>
<Content><!]></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><!]></ToUserName>
<FromUserName><!]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><!]></MsgType>
<ArticleCount><!]></ArticleCount>
<Articles>";//与下面的picTpl拼接
$picTpl = "<item>
<Title><!]></Title>
<Description><!]></Description>
<PicUrl><!]></PicUrl>
<Url><!]></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='http://www.52pojie.cn/static/image/common/logo.png';//请改为自己的默认图片
$url='http://so.52pojie.cn/cse/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://so.52pojie.cn/cse/search?q=?key=$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='http://api.map.baidu.com/telematics/v3/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"]["weather_data"];
}
这开发的是什么内容? 关注后自动回复and查天气... Hmily 发表于 2015-9-7 10:30
这开发的是什么内容?
1、关注会自动发一条消息
2、回复常用的“你好”“喂”之类的会推荐一条自动回答。
3、回复“北京天气”之类会显示最近几天的天气
4、当2、3条件不满足的时候从设置的网站抓取相关的内容(相当于搜索网站内容),然后将得到的结果的前面几条推送给用户。
5、会在推送的内容最后加一条如果“没有找到合适的内容?点这里查看更多”,可以链接到某个网址。
下面是抓取天气、和解析xml的函数。 ID:遭遇花无缺
邮箱:52023374@qq.com
申请通过,欢迎光临吾爱破解论坛,期待吾爱破解有你更加精彩,ID和密码自己通过邮件密码找回功能修改,请即时登陆并修改密码!
登陆后请在一周内在此帖报道,否则将删除ID信息。 恭喜 恭喜 {:301_997:} 报到,已登录并修改相关信息。
页:
[1]