日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php微信公众号获取天气预报,【微信公众平台开发】封装获取天气预报功能

發布時間:2025/4/5 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php微信公众号获取天气预报,【微信公众平台开发】封装获取天气预报功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

header('Content-Type:text/html;charset=utf-8');

class WeChatMsgType

{

private $toUserName;

private $fromUserName;

private $xmlModle;

private $createTime;

public function SetUserInfo($toUserName,$fromUserName) //設置用戶信息

{

$this->toUserName = $toUserName;

$this->fromUserName = $fromUserName;

$this->createTime = time();

}

public function WeatherMsg($cityName)//天氣預報信息

{

$url = "http://api.map.baidu.com/telematics/v3/weather?location=".$cityName."&output=json&ak=11ffd27d38deda622f51c9d314d46b17";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

curl_close($ch);

$result = json_decode($output, true);

if ($result["error"] != 0){

return $result["status"];

}

$curHour = (int)date('H',time());

$weather = $result["results"][0];

$ImageTexts = array();

$ImageTexts[0] = self::SetImageTextInfo($weather['currentCity'].'天氣預報',"", "", "");

//$aa=var_export(urldecode(urlencode($weather)),true);

//file_put_contents("debug.txt", date("Y-m-d H:i:s",time()).$aa.PHP_EOL,FILE_APPEND);

for($i=0; $i

{

$ImageTexts[$i+1] = self::SetImageTextInfo($weather["weather_data"][$i]["date"]."\n".

$weather["weather_data"][$i]["weather"]." ".

$weather["weather_data"][$i]["wind"]." ".

$weather["weather_data"][$i]["temperature"],

"", (($curHour >= 6) && ($curHour < 18))?$weather["weather_data"][$i]["dayPictureUrl"]:$weather["weather_data"][$i]["nightPictureUrl"], "");

}

return self::ImageTextMsg($ImageTexts);

}

public function SetImageTextInfo($sTitle, $sDescription, $sPicUrl, $Url)

{

$item = "

";

$ret=sprintf($item, $sTitle, $sDescription, $sPicUrl,$Url);

return $ret;

}

public function ImageTextMsg($ImageTexts)

{

$this->xmlModle = "

%s

%d

";

foreach ($ImageTexts as $item)

{

$this->xmlModle = $this->xmlModle.$item;

}

$this->xmlModle = $this->xmlModle."" ;

$ret=sprintf($this->xmlModle, $this->fromUserName, $this->toUserName, $this->createTime,count($ImageTexts));

return $ret;

}

}

?>

調用用法:

總結

以上是生活随笔為你收集整理的php微信公众号获取天气预报,【微信公众平台开发】封装获取天气预报功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。