PHP code example of jhapis / weather

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

    

jhapis / weather example snippets


use Weather\Apis\ChinaAirQuality;

y' => '聚合接口申请Key']);

// 支持省份列表
$result = $wether->apiGetway('provinceList');
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 省份支持城市城市:省份列表ID
$params = ['pId' => 10];
$result = $wether->apiGetway('cityList', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

//城市实时空气质量
$params = ['cityId' => 1];
$result = $wether->apiGetway('cityAirQuality', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 城市站点实时空气质量
$params = ['cityId' => 1];
$result = $wether->apiGetway('siteAirQuality', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 城市,近14天空气质量
$params = ['cityId' => 1];
$result = $wether->apiGetway('daysAirQuality', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 城市,近24小时空气质量
$params = ['cityId' => 1];

$result = $wether->apiGetway('hoursAirQuality',$params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";


use Weather\Apis\Weather;

y' => '聚合接口申请Key']);

$params = ['city' => '苏州'];
$result = $wether->apiGetway('cityWeather', $params);
var_dump($result);

$params = ['city' => '苏州'];
$result = $wether->apiGetway('eatherIndex', $params);
var_dump($result);

// 天气种类列表
$result = $wether->apiGetway('weatherType');
var_dump($result);


use Weather\Apis\HistoryWeatherQuery;

y' => '聚合接口申请Key']);

// 支持省份查询
$result = $wether->apiGetway('provinceList');
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 支持城市查询
$params = ['province_id' => 16];
$result = $wether->apiGetway('cityList', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 历史天气查询
$params = ['city_id' => 1145,'weather_date'=>'2011-01-01'];

$result = $wether->apiGetway('historyWeather',$params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";


$wether = new AirQuality(['key' => '聚合接口申请Key']);

// 城市空气质量
$params = ['city' => '苏州'];
$result = $wether->apiGetway('cityAirQuality', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 支持城市列表
$result = $wether->apiGetway('cityList');
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 城市空气质量PM2。5
$params = ['city' => '苏州'];
$result = $wether->apiGetway('cityAirPM', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// pm支持城市
$result = $wether->apiGetway('pmCityList');
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";


use Weather\Apis\ChinaWeather;

 new ChinaWeather(['key' => '聚合接口申请Key']);

// 支持城市列表
$citys = $wether->apiGetway('citys');
echo json_encode($citys, JSON_UNESCAPED_UNICODE)."\n\n";

// 天气种类及标识列表
$weatherTips = $wether->apiGetway('weatherTips');
echo json_encode($weatherTips, JSON_UNESCAPED_UNICODE)."\n\n";

// 根据城市名称或城市ID查询天气
$params = ['cityname' => '苏州'];
$params = ['cityname' => '1145'];
$result = $wether->apiGetway('cityWeather', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

use Weather\Apis\HistoryWeather;

y' => '聚合接口申请Key']);

// 支持省份查询
$result = $wether->apiGetway('provinceList');
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 支持城市查询
$params = ['province_id' => 16];
$result = $wether->apiGetway('cityList', $params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";

// 历史天气查询
$params = ['city_id' => 1145,'weather_date'=>'2011-01-01'];

$result = $wether->apiGetway('historyWeather',$params);
echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n\n";
json
// 城市天气
{
	"reason": "查询成功!",
	"result": {
		"city": "苏州",
		"realtime": {
			"temperature": "32",
			"humidity": "33",
			"info": "晴",
			"wid": "00",
			"direct": "南风",
			"power": "3级",
			"aqi": "114"
		},
		"future": [{
			"date": "2025-03-26",
			"temperature": "21\/32℃",
			"weather": "晴转中雨",
			"wid": {
				"day": "00",
				"night": "08"
			},
			"direct": "南风转西南风"
		},
        ....
        ]
	},
	"error_code": 0
}
json
// 空气质量PM2.5
{
  "resultcode": "200",
  "reason": "SUCCESSED!",
  "result": [
    {
      "city": "苏州",
      "PM2.5": "50",
      "AQI": "114",
      "quality": "轻度污染",
      "PM10": "90",
      "CO": "0.0",
      "NO2": "23",
      "O3": "229",
      "SO2": "9",
      "time": "2025-03-26 16:51:13"
    }
  ],
  "error_code": 0
}
json
// 历史天气查询结果
{
  "reason": "查询成功",
  "result": {
    "city_id": "1145",
    "city_name": "南京",
    "weather_date": "2011-01-01",
    "day_weather": "晴",
    "night_weather": "晴",
    "day_temp": "4℃",
    "night_temp": "-4℃",
    "day_wind": "北风",
    "day_wind_comp": "3-4级",
    "night_wind": "东北风",
    "night_wind_comp": "4-5级",
    "day_weather_id": "00",
    "night_weather_id": "00"
  },
  "error_code": 0
}