PHP 获取天气

    /**
     * 获取天气
     */
    function get_tq () {

        //获取用户ip地址
        $ip = get_real_ip();
        // $ip = '123.125.71.38';
        //根据ip地址 获取用户所在城市
        $api_key = '8d6c8b8f3749aed6b1aff3aad6f40e37';           
        // 使用固定城市北京市
        $cityName = '北京市';

        //获取今天的日期
        $day = time_format(NOW_TIME, 'Y.m.d');

        if( $cityName ) {
            $tq_datas = S('site.widget.nav.tq.' . md5($cityName) . '.day.' . $day );

            if( false == $tq_datas ){
                //根据城市名 获取天气情况
                $tq_datas = @file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=" . urlencode($cityName) . "&output=json&ak=" . $api_key);
                $tq_datas = json_decode( $tq_datas, true );

                $tq_datas = $tq_datas ? $tq_datas : [];
                S('site.widget.nav.tq.' . md5($cityName) . '.day.' . $day, $tq_datas );
            }
        }

        foreach( $tq_datas['results'][0]['weather_data'] as $key=>$val ) {
            $tiqi_name = '';
            $tiqi_name = explode( '/',$val['dayPictureUrl']);
            $tiqi_name = explode( '.', end( $tiqi_name))[0];

            $tq_lists[ $key ] = $val;
            $terminal = cookie('terminal');

            if( $terminal == 'wap') {
                $tq_lists[ $key ]['ico'] = '/themes_wap/tainqi/' . $tiqi_name . ".png";
            } else {
                $tq_lists[ $key ]['ico'] = '/themes/tainqi/' . $tiqi_name . ".png";
            }
        }

        $tq_lists[0]['cityName'] = $cityName;
        if ( $terminal == 'default' ) {
            $this->tq_lists = $tq_lists;
        } else {
            $this->tq_lists = $tq_lists[0];
        }
        
        $this->ajaxReturn([ 'status' => 1 , 'name' => $tq_lists ] );
    }

 

转载于:https://www.cnblogs.com/ningjiabing/p/10558888.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值