概述
<?php
/*
CopyRight 2013 www.doucube.com All Rights Reserved
*/
//traceHttp();
define("TOKEN", "weixin");
$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 = file_get_contents('php://input');
if (!empty($postStr)) {
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);//text image link event location
switch ($RX_TYPE) {
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: " . $RX_TYPE;
break;
}
echo $resultStr;
} else {
echo "no";
exit;
}
}
function handleText($postObj){
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$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>";
$msgType = "text";
if($keyword == "hello")
{
$contentStr='你好!';
}elseif($keyword=='时间'){
$contentStr = date("Y-m-d H:i:s",time());
}else{
$url="http://api.k780.com/?app=weather.future&weaid=$keyword&appkey=23484&sign=e88842ebf17cafdd109f6277ad58b01c&format=json";
$data=file_get_contents($url);
$data=json_decode($data,true);
$contentStr="未来一周天气情况:日期:".$data['result'][0]['days'].";星期:".$data['result'][0]['week'].";城市:".$data['result'][0]['citynm'].";温度:".$data['result'][0]['temperature'].";天气情况:".$data['result'][0]['weather'].";风向:".$data['result'][0]['wind'].";风力:".$data['result'][0]['winp'].";最高温度:".$data['result'][0]['temp_high']."℃;最低温度:".$data['result'][0]['temp_low']."℃____________________________";
$contentStr.="日期:".$data['result'][1]['days'].";星期:".$data['result'][1]['week'].";城市:".$data['result'][1]['citynm'].";温度:".$data['result'][1]['temperature'].";天气情况:".$data['result'][1]['weather'].";风向:".$data['result'][1]['wind'].";风力:".$data['result'][1]['winp'].";最高温度:".$data['result'][1]['temp_high']."℃;最低温度:".$data['result'][1]['temp_low']."℃____________________________";
$contentStr.="日期:".$data['result'][2]['days'].";星期:".$data['result'][2]['week'].";城市:".$data['result'][2]['citynm'].";温度:".$data['result'][2]['temperature'].";天气情况:".$data['result'][2]['weather'].";风向:".$data['result'][2]['wind'].";风力:".$data['result'][2]['winp'].";最高温度:".$data['result'][2]['temp_high']."℃;最低温度:".$data['result'][2]['temp_low']."℃____________________________";
$contentStr.="日期:".$data['result'][3]['days'].";星期:".$data['result'][3]['week'].";城市:".$data['result'][3]['citynm'].";温度:".$data['result'][3]['temperature'].";天气情况:".$data['result'][3]['weather'].";风向:".$data['result'][3]['wind'].";风力:".$data['result'][3]['winp'].";最高温度:".$data['result'][3]['temp_high']."℃;最低温度:".$data['result'][3]['temp_low']."℃____________________________";
$contentStr.="日期:".$data['result'][4]['days'].";星期:".$data['result'][4]['week'].";城市:".$data['result'][4]['citynm'].";温度:".$data['result'][4]['temperature'].";天气情况:".$data['result'][4]['weather'].";风向:".$data['result'][4]['wind'].";风力:".$data['result'][4]['winp'].";最高温度:".$data['result'][4]['temp_high']."℃;最低温度:".$data['result'][4]['temp_low']."℃____________________________";
$contentStr.="日期:".$data['result'][5]['days'].";星期:".$data['result'][5]['week'].";城市:".$data['result'][5]['citynm'].";温度:".$data['result'][5]['temperature'].";天气情况:".$data['result'][5]['weather'].";风向:".$data['result'][5]['wind'].";风力:".$data['result'][5]['winp'].";最高温度:".$data['result'][5]['temp_high']."℃;最低温度:".$data['result'][5]['temp_low']."℃____________________________";
$contentStr.="日期:".$data['result'][6]['days'].";星期:".$data['result'][6]['week'].";城市:".$data['result'][6]['citynm'].";温度:".$data['result'][6]['temperature'].";天气情况:".$data['result'][6]['weather'].";风向:".$data['result'][6]['wind'].";风力:".$data['result'][6]['winp'].";最高温度:".$data['result'][6]['temp_high']."℃;最低温度:".$data['result'][6]['temp_low']."℃____________________________";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
function handleEvent($object){
$contentStr="";
switch ($object->Event)
{
case "subscribe":
$contentStr=array(
'title'=>'渲染笔墨情',
'content'=>'本公司成立于2017年,主要经营与互联网相关的业务...',
'pic'=>'http://dsf.hk137.com/images/test.jpg',
'url'=>'http://m.cnblogs.com/?u=txw1958'
);
break;
case "CLICK":
switch($object->EventKey)
{
case "company_intro":
$contentStr=array(
'title'=>'公司简介',
'content'=>'本公司成立于2017年,主要经营与互联网相关的业务...',
'pic'=>'http://1158410.sqnet.cn/ge/images/zgf.jpg',
'url'=>'1158410.sqnet.cn/ge/weixin01.html'
);
break;
case "weather":
$contentStr="请输入要查询的城市名称..";
break;
case "contact":
$contentStr="联系人:公司老总 ,CEO X先生;联系电话:......";
break;
default :
break;
}
break;
default :
$resultStr = "Unknow Event: ".$object->Event;
break;
}
if(is_array($contentStr)){
$resultStr = $this->responseNews($object, $contentStr);
}else{
$resultStr = $this->responseText($object,$contentStr);
}
return $resultStr;
}
//关注 简介回复
function responseNews($object,$arr,$flag=0){
$newsTplHead = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>";
$newsTplBody = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$newsTplFoot = "</Articles>
<FuncFlag>0</FuncFlag>
</xml>";
$resultStr = sprintf($newsTplHead, $object->FromUserName, $object->ToUserName, time());
$resultStr.=sprintf($newsTplBody, $arr["title"], $arr['content'], $arr['pic'],$arr['url']);
$resultStr.=$newsTplFoot;
return $resultStr;
}
//点击事件回复
function responseText($object,$contentStr){
$fromUsername = $object->FromUserName;
$toUsername = $object->ToUserName;
$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>";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
return $resultStr;
}
}
function traceHttp()
{
logger("nnREMOTE_ADDR:".$_SERVER["REMOTE_ADDR"].(strstr($_SERVER["REMOTE_ADDR"],'101.226')? " FROM WeiXin": "Unknown IP"));
logger("QUERY_STRING:".$_SERVER["QUERY_STRING"]);
}
function logger($log_content)
{
if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
sae_set_display_errors(false);
sae_debug($log_content);
sae_set_display_errors(true);
}else{ //LOCAL
$max_size = 500000;
$log_filename = "log.xml";
if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."rn", FILE_APPEND);
}
}
?>
最后
以上就是单身煎蛋为你收集整理的关注回复图文,天气查询的全部内容,希望文章能够帮你解决关注回复图文,天气查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复