PHP code example of fantasyuknow / weather

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

    

fantasyuknow / weather example snippets

 php
    use Shenguowei\Weather\Weather;
  
    $key = 'xxxxxxxxxxx';//API Key

    $w = new Weather($key);
  
    echo "获取实时天气:\n";
  
    $response = $w->getWeather('合肥');
    echo json_encode($response,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  
    echo "\n获取天气预报:\n";
    $response = $w->getWeather('合肥','forecast');
    echo json_encode($response,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  
    echo "\n获取实时天气(XML)\n";
    echo $w->getWeather('合肥','live','XML');

   返回示例:

   {
       "status": "1",
       "count": "1",
       "info": "OK",
       "infocode": "10000",
       "lives": [
           {
               "province": "安徽",
               "city": "合肥市",
               "adcode": "340100",
               "weather": "多云",
               "temperature": "28",
               "winddirection": "北",
               "windpower": "≤3",
               "humidity": "81",
               "reporttime": "2020-07-01 11:24:12"
           }
       ]
   }