PHP code example of sylvia / weather

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

    

sylvia / weather example snippets


use Sylvia\Weather\Weather;

$key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';

$weather = new Weather($key);

$response = $weather->getWeather('上海');

{
  "status" => "1",
  "count" => "1",
  "info" => "OK",
  "infocode" => "10000",
  "lives" => [
    {
      "province" => "上海"
      "city" => "上海市"
      "adcode" => "310000"
      "weather" => "阴"
      "temperature" => "7"
      "winddirection" => "北"
      "windpower" => "≤3"
      "humidity" => "83"
      "reporttime" => "2019-01-15 10:45:47"
    }
  ]
}

{
  "status" => "1",
  "count" => "1",
  "info" => "OK",
  "infocode" => "10000",
  "forecasts" =>[
  {
      "city" => "上海市",
      "adcode" => "310000",
      "province" => "上海",
      "reporttime" => "2019-01-15 10:45:47",
      "casts" => [
        {
          "date" => "2019-01-15"
          "week" => "2"
          "dayweather" => "小雨"
          "nightweather" => "多云"
          "daytemp" => "8"
          "nighttemp" => "2"
          "daywind" => "北"
          "nightwind" => "北"
          "daypower" => "5"
          "nightpower" => "5"
        },
        {
          "date" => "2019-01-16"
          "week" => "3"
          "dayweather" => "多云"
          "nightweather" => "多云"
          "daytemp" => "5"
          "nighttemp" => "0"
          "daywind" => "北"
          "nightwind" => "北"
          "daypower" => "≤3"
          "nightpower" => "≤3"
        },
        {
          "date" => "2019-01-17"
          "week" => "4"
          "dayweather" => "晴"
          "nightweather" => "晴"
          "daytemp" => "7"
          "nighttemp" => "1"
          "daywind" => "西北"
          "nightwind" => "西北"
          "daypower" => "≤3"
          "nightpower" => "≤3"
        },
        {
          "date" => "2019-01-18"
          "week" => "5"
          "dayweather" => "晴"
          "nightweather" => "多云"
          "daytemp" => "10"
          "nighttemp" => "5"
          "daywind" => "东南"
          "nightwind" => "东南"
          "daypower" => "4"
          "nightpower" => "4"
        }
      ]
  }]
}

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

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

	.
	.
	.
	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>