PHP code example of chenshuai1993 / weather

1. Go to this page and download the library: Download chenshuai1993/weather library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chenshuai1993 / weather example snippets


use Chenshuai1993\Weather\Weather;
$key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
$weather = new Weather($key);

// 获取实时天气
$response = $w->getLiveWeather('深圳');

获取实时天气:
{
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "lives": [
        {
            "province": "广东",
            "city": "深圳市",
            "adcode": "440300",
            "weather": "阴",
            "temperature": "21",
            "winddirection": "东南",
            "windpower": "≤3",
            "humidity": "91",
            "reporttime": "2019-05-05 09:44:31"
        }
    ]
}

$response = $weather->getForecastsWeather('深圳');

获取天气预报:
{
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "forecasts": [
        {
            "city": "深圳市",
            "adcode": "440300",
            "province": "广东",
            "reporttime": "2019-05-05 09:44:31",
            "casts": [
                {
                    "date": "2019-05-05",
                    "week": "7",
                    "dayweather": "大雨",
                    "nightweather": "中雨",
                    "daytemp": "22",
                    "nighttemp": "19",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "5",
                    "nightpower": "5"
                },
                {
                    "date": "2019-05-06",
                    "week": "1",
                    "dayweather": "雷阵雨",
                    "nightweather": "中雨",
                    "daytemp": "25",
                    "nighttemp": "19",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2019-05-07",
                    "week": "2",
                    "dayweather": "雷阵雨",
                    "nightweather": "雷阵雨",
                    "daytemp": "23",
                    "nighttemp": "20",
                    "daywind": "东",
                    "nightwind": "东",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2019-05-08",
                    "week": "3",
                    "dayweather": "暴雨",
                    "nightweather": "暴雨",
                    "daytemp": "25",
                    "nighttemp": "21",
                    "daywind": "无风向",
                    "nightwind": "无风向",
                    "daypower": "≤3",
                    "nightpower": "≤3"
                }
            ]
        }
    ]
}


$response = $weather->getLiveWeather('深圳', 'xml')

    use Chenshuai1993\Weather\Weather;
    .
    .
    .
    public function edit(Weather $weather) 
    {
        $response = $weather->getLiveWeather('深圳');
    }
    .
    .
    .

    use Chenshuai1993\Weather\Weather;
    .
    .
    .
    public function edit() 
    {
        $response = app('weather')->getLiveWeather('深圳');
    }
    .
    .
    .
$xslt
path/config/services.php

'weather' => [
        'key' => env('WEATHER_API_KEY'),
    ],