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;

}

}

?>

调用用法:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值