php 获取天气信息

php获取天气信息,记录下接口,自己定义一下需要的格式,写篇博客,以便备用

<?php
/**
 * Created by PhpStorm.
 * User: ljp
 * Date: 2017/12/8
 * Time: 上午9:04
 */

function tianqi($chengshi)
{
    $url = 'http://wthrcdn.etouch.cn/weather_mini?city='.urlencode($chengshi);
    $html = file_get_contents($url);
    $jsondata = gzdecode($html);
    $data=json_decode($jsondata,true);

    $arr=array();
    $arr['chengshi']=$data['data']['city'];
    $dangtian=$data['data']['forecast'][0];
    $arr['gaowen']= str_replace("高温 ",null,$dangtian['high']);
    $arr['diwen']= str_replace("低温 ",null,$dangtian['low']);
    $arr['tianqi']=$dangtian['type'];
    return $arr;


}

print_r(tianqi('秦皇岛'));

posted @ 2017-12-08 10:12  itlijinpeng  阅读(565)  评论(0编辑  收藏  举报