700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 微信查询天气php程序 微信公众号开发-输入城市查询天气

微信查询天气php程序 微信公众号开发-输入城市查询天气

时间:2022-03-20 15:54:49

相关推荐

微信查询天气php程序 微信公众号开发-输入城市查询天气

/**

* 查询天气 输入城市名称比如:北京,即可返回天气信息

*/

//define your token

define("TOKEN", "******");//换上你自己的token

$wechatObj = new wechatCallbackapiTest();

if (!isset($_GET['echostr'])) {

$wechatObj->responseMsg();

}else{

$wechatObj->valid();

}

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);

//发送消息方ID

$fromUsername = $postObj->FromUserName;

//接收消息方ID

$toUsername = $postObj->ToUserName;

//消息类型

$form_MsgType = $postObj->MsgType;

$keyword = trim($postObj->Content);

$time = time();

$textTpl = "%s0";

$tqTpl = "%s5 ";

//事件消息

if ($form_MsgType == "text") {

//获取用户发送的文字内容

$city = trim($postObj->Content);

if (!empty($city)){

$mykey = "**************************";//换上你自己的key

$url = "http://api./telematics/v3/weather?location=".$city."&output=json&ak=".$mykey;

$output = file_get_contents($url);

$contentStr = json_decode($output, true);

if($contentStr['status']=='success')

{

$T[0]['Title']=$contentStr['date']." ".$contentStr['results'][0]['currentCity']."天气";

//$T[0]['PicUrl']="http://zhengwairen-try2./xytq.jpg";

//$T[0]['Url']="http://zhengwairen-try2./xytq.jpg";

//上面两行可以设成你想要的顶部图片

if(is_array($contentStr['results'][0]['index']))

{

$T[2]['Title']="【pm2.5】".$contentStr['results'][0]['pm25']."\n"."【".$contentStr['results'][0]['index'][0]['title']."】"."(".$contentStr['results'][0]['index'][0]['zs'].") ".$contentStr['results'][0]['index'][0]['des'];

//下一行是洗车指数,感觉不对主题还是不要的好。。

//$T[2]['Title']=$T[2]['Title']."\n"."【".$contentStr['results'][0]['index'][1]['title']."】(".$contentStr['results'][0]['index'][1]['zs'].") ".$contentStr['results'][0]['index'][1]['des'];

//下一行是感冒指数

$T[2]['Title']=$T[2]['Title']."\n"."【".$contentStr['results'][0]['index'][2]['title']."】(".$contentStr['results'][0]['index'][2]['zs'].") ".$contentStr['results'][0]['index'][2]['des'];

//下一行是运动指数

$T[2]['Title']=$T[2]['Title']."\n"."【".$contentStr['results'][0]['index'][3]['title']."】(".$contentStr['results'][0]['index'][3]['zs'].") ".$contentStr['results'][0]['index'][3]['des'];

//下一行是紫外线指数

$T[2]['Title']=$T[2]['Title']."\n"."【".$contentStr['results'][0]['index'][4]['title']."】(".$contentStr['results'][0]['index'][4]['zs'].") ".$contentStr['results'][0]['index'][4]['des'];

}

else

$guowai=1;

for($i=1,$aaa=0;$i<=5;$i++)

{

if($i==2 && $guowai!=1)

continue;

if($guowai==1 && $i==5)

continue;

$T[$i]['Title']=$contentStr['results'][0]['weather_data'][$aaa]['date']." ".$contentStr['results'][0]['weather_data'][$aaa]['temperature']." ".$contentStr['results'][0]['weather_data'][$aaa]['weather']." ".$contentStr['results'][0]['weather_data'][$aaa]['wind'];

$T[$i]['PicUrl']=$contentStr['results'][0]['weather_data'][$aaa]['dayPictureUrl'];

$T[$i]['Url']=$contentStr['results'][0]['weather_data'][$aaa]['dayPictureUrl'];

$aaa++;

}

$resultStr = sprintf($tqTpl,$fromUsername,$toUsername,time(),"news",$T[0]['Title'],$T[0]['Description'],$T[0]['PicUrl'],$T[0]['Url'], $T[1]['Title'],$T[1]['Description'],$T[1]['PicUrl'],$T[1]['Url'], $T[2]['Title'],$T[2]['Description'],$T[2]['PicUrl'],$T[2]['Url'], $T[3]['Title'],$T[3]['Description'],$T[3]['PicUrl'],$T[3]['Url'], $T[4]['Title'],$T[4]['Description'],$T[4]['PicUrl'],$T[4]['Url'],$T[5]['Title'],$T[5]['Description'],$T[5]['PicUrl'],$T[5]['Url']);

//echo $resultStr;

}

}

echo $resultStr;

exit;

//事件消息

}elseif($form_MsgType=="event")

{

//获取事件类型

$form_Event = $postObj->Event;

//订阅事件

if($form_Event=="subscribe")

{

//回复欢迎文字消息

$msgType = "text";

$contentStr = "感谢您关注Super King的天气查询公众号[玫瑰]。请输入要查询的城市名称,比如:北京";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $contentStr);

echo $resultStr;

exit;

}

}

}else {

echo "";

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;

}

}

}

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。