微信公众号平台天气预报开发

获取天气的代码,然后方法放在微信中调用便可
<?php
    function httpRequest($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        if ($output === FALSE){
            return "cURL Error: ". curl_error($ch);
        }
        return $output;
    }

    function getcity($add){
        include 'conn.inc.php';   //调用连接数据库的php文件
        mysql_set_charset("utf8");
        mysql_select_db($dbname);
        mysql_query("set names utf8");

        $sql = "select cityCode from weather where cityName = '$add'";
        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);
        return $row['cityCode'];
    }
    
    //根据citycode获取天气接口的数据
    function getWeatherInfo($cityCode){    
        //获取实时天气
        $url = "http://www.weather.com.cn/data/sk/".$cityCode.".html";
        $output = httpRequest($url);
        $weather = json_decode($output, true);
        $info = $weather['weatherinfo'];

        $weatherArray = array();
        $weatherArray[] = array("Title"=>$info['city']."现在的天气预报", "Description"=>"", "PicUrl"=>"", "Url" =>"");
        if ((int)$cityCode < 101340000){
            $result = "实况 温度:".$info['temp']."℃ 湿度:".$info['SD']." 风速:".$info['WD'].$info['WSE']."级";
            $weatherArray[] = array("Title"=>str_replace("%", "﹪", $result), "Description"=>"", "PicUrl"=>"", "Url" =>"");
        }

        //获取六日天气
        $url = "http://m.weather.com.cn/data/".$cityCode.".html";
        $output = httpRequest($url);
        $weather = json_decode($output, true);
        $info = $weather['weatherinfo'];

        if (!empty($info['index_d'])){
            $weatherArray[] = array("Title" =>$info['index_d'], "Description" =>"", "PicUrl" =>"", "Url" =>"");
        }

        $weekArray = array("日","一","二","三","四","五","六");
        $maxlength = 3;
        for ($i = 1; $i <= $maxlength; $i++) {
            $offset = strtotime("+".($i-1)." day");
            $subTitle = date("m月d日",$offset)." 周".$weekArray[date('w',$offset)]." ".$info['temp'.$i]." ".$info['weather'.$i]." ".$info['wind'.$i];
            $weatherArray[] = array("Title" =>$subTitle, "Description" =>"", "PicUrl" =>"http://discuz.comli.com/weixin/weather/"."d".sprintf("%02u",$info['img'.(($i *2)-1)]).".jpg", "Url" =>"");
        }

        return $weatherArray;
    }




?>


参考链接:http://www.cnblogs.com/txw1958/p/weixin-weather-forecast.html




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值