PHP code example of xin6841414 / weather

1. Go to this page and download the library: Download xin6841414/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/ */

    

xin6841414 / weather example snippets


use Xin6841414\Weather\Weather;

$key = 'XXXXXXXXXXXXXXXXX';

$weather = new Weather($key);


$response = $weather->getWeather('青岛');


{
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "lives": [
        {
            "province": "山东",
            "city": "青岛市",
            "adcode": "370200",
            "weather": "阴",
            "temperature": "16",
            "winddirection": "北",
            "windpower": "≤3",
            "humidity": "39",
            "reporttime": "2020-10-13 08:25:20"
        }
    ]
}

$response = $weather->getWeather('青岛', 'all');


{
    "status": "1",
    "count": "1",
    "info": "OK",
    "infocode": "10000",
    "forecasts": [
        {
            "city": "青岛市",
            "adcode": "370200",
            "province": "山东",
            "reporttime": "2020-10-13 09:25:18",
            "casts": [
                {
                    "date": "2020-10-13",
                    "week": "2",
                    "dayweather": "多云",
                    "nightweather": "小雨",
                    "daytemp": "18",
                    "nighttemp": "14",
                    "daywind": "南",
                    "nightwind": "南",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2020-10-14",
                    "week": "3",
                    "dayweather": "小雨",
                    "nightweather": "阴",
                    "daytemp": "16",
                    "nighttemp": "13",
                    "daywind": "北",
                    "nightwind": "北",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2020-10-15",
                    "week": "4",
                    "dayweather": "多云",
                    "nightweather": "小雨",
                    "daytemp": "17",
                    "nighttemp": "14",
                    "daywind": "南",
                    "nightwind": "南",
                    "daypower": "4",
                    "nightpower": "4"
                },
                {
                    "date": "2020-10-16",
                    "week": "5",
                    "dayweather": "小雨",
                    "nightweather": "阴",
                    "daytemp": "20",
                    "nighttemp": "10",
                    "daywind": "西北",
                    "nightwind": "西北",
                    "daypower": "4",
                    "nightpower": "4"
                }
            ]
        }
    ]
}


$response = $weather->getWeather('深圳', 'all', 'xml');


array|string getWeather(string $city, string $type='base', string $format = 'json')

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


WEATHER_API_KEY=XXXXXXXXXXXXXXXXXXXXX

    .
    .
    .
    public function edit(Weather $weather)
    {
        $response = $weather->getWeather('深圳');
    }
    


    .
    .
    .
    public function edit() 
    {
        $response = app('weather')->getWeather('深圳');
    }
    .
    .
    .
xml
<response>
    <status>1</status>
    <count>1</count>
    <info>OK</info>
    <infocode>10000</infocode>
    <lives type="list">
        <live>
            <province>广东</province>
            <city>深圳市</city>
            <adcode>440300</adcode>
            <weather>中雨</weather>
            <temperature>27</temperature>
            <winddirection>西南</winddirection>
            <windpower>5</windpower>
            <humidity>94</humidity>
            <reporttime>2018-08-21 16:00:00</reporttime>
        </live>
    </lives>
</response>