十五天天气预报
其他 官方文档
采用城市ID来精准查询15天内的天气,接口每8小时更新一次
基本说明:
接口地址:http://t.weather.sojson.com/api/weather
返回格式:json
请求方式:get
请求示例:http://t.weather.sojson.com/api/weather/city/101030100
请求参数说明:
名称 类型 必填 说明
city string 必填 要查询天气的城市ID
返回参数说明:
名称 类型 说明
- - -
JSON返回示例:
{
	"message": "success感谢又拍云(upyun.com)提供CDN赞助",
	"status": 200,
	"date": "20191004",
	"time": "2019-10-04 10:28:23",
	"cityInfo": {
		"city": "天津市",
		"citykey": "101030100",
		"parent": "天津",
		"updateTime": "09:21"
	},
	"data": {
		"shidu": "72%",
		"pm25": 17.0,
		"pm10": 62.0,
		"quality": "良",
		"wendu": "13",
		"ganmao": "极少数敏感人群应减少户外活动",
		"forecast": [{
			"date": "04",
			"high": "高温 16℃",
			"low": "低温 10℃",
			"ymd": "2019-10-04",
			"week": "星期五",
			"sunrise": "06:08",
			"sunset": "17:51",
			"aqi": 45,
			"fx": "东北风",
			"fl": "5-6级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "05",
			"high": "高温 18℃",
			"low": "低温 10℃",
			"ymd": "2019-10-05",
			"week": "星期六",
			"sunrise": "06:09",
			"sunset": "17:50",
			"aqi": 55,
			"fx": "北风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "06",
			"high": "高温 20℃",
			"low": "低温 13℃",
			"ymd": "2019-10-06",
			"week": "星期日",
			"sunrise": "06:10",
			"sunset": "17:48",
			"aqi": 75,
			"fx": "西南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "07",
			"high": "高温 23℃",
			"low": "低温 12℃",
			"ymd": "2019-10-07",
			"week": "星期一",
			"sunrise": "06:11",
			"sunset": "17:47",
			"aqi": 90,
			"fx": "西南风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "08",
			"high": "高温 22℃",
			"low": "低温 12℃",
			"ymd": "2019-10-08",
			"week": "星期二",
			"sunrise": "06:12",
			"sunset": "17:45",
			"aqi": 95,
			"fx": "东南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "09",
			"high": "高温 24℃",
			"low": "低温 13℃",
			"ymd": "2019-10-09",
			"week": "星期三",
			"sunrise": "06:13",
			"sunset": "17:44",
			"aqi": 101,
			"fx": "西南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "10",
			"high": "高温 25℃",
			"low": "低温 14℃",
			"ymd": "2019-10-10",
			"week": "星期四",
			"sunrise": "06:14",
			"sunset": "17:42",
			"fx": "西南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "11",
			"high": "高温 21℃",
			"low": "低温 11℃",
			"ymd": "2019-10-11",
			"week": "星期五",
			"sunrise": "06:15",
			"sunset": "17:41",
			"fx": "东风",
			"fl": "3-4级",
			"type": "阴",
			"notice": "不要被阴云遮挡住好心情"
		}, {
			"date": "12",
			"high": "高温 20℃",
			"low": "低温 11℃",
			"ymd": "2019-10-12",
			"week": "星期六",
			"sunrise": "06:16",
			"sunset": "17:39",
			"fx": "北风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "13",
			"high": "高温 23℃",
			"low": "低温 13℃",
			"ymd": "2019-10-13",
			"week": "星期日",
			"sunrise": "06:17",
			"sunset": "17:38",
			"fx": "东南风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "14",
			"high": "高温 26℃",
			"low": "低温 15℃",
			"ymd": "2019-10-14",
			"week": "星期一",
			"sunrise": "06:18",
			"sunset": "17:36",
			"fx": "东南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "15",
			"high": "高温 27℃",
			"low": "低温 16℃",
			"ymd": "2019-10-15",
			"week": "星期二",
			"sunrise": "06:19",
			"sunset": "17:35",
			"fx": "南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "16",
			"high": "高温 23℃",
			"low": "低温 10℃",
			"ymd": "2019-10-16",
			"week": "星期三",
			"sunrise": "06:20",
			"sunset": "17:33",
			"fx": "北风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "17",
			"high": "高温 17℃",
			"low": "低温 10℃",
			"ymd": "2019-10-17",
			"week": "星期四",
			"sunrise": "06:21",
			"sunset": "17:32",
			"fx": "东南风",
			"fl": "<3级",
			"type": "小雨",
			"notice": "雨虽小,注意保暖别感冒"
		}, {
			"date": "18",
			"high": "高温 20℃",
			"low": "低温 10℃",
			"ymd": "2019-10-18",
			"week": "星期五",
			"sunrise": "06:22",
			"sunset": "17:30",
			"fx": "南风",
			"fl": "3-4级",
			"type": "阴",
			"notice": "不要被阴云遮挡住好心情"
		}],
		"yesterday": {
			"date": "03",
			"high": "高温 30℃",
			"low": "低温 15℃",
			"ymd": "2019-10-03",
			"week": "星期四",
			"sunrise": "06:07",
			"sunset": "17:53",
			"aqi": 112,
			"fx": "东风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}
	}
}
服务级错误码参照
错误码 说明
403 参数错误返回值
404 参数数位不对返回值
完整教学代码示例
<?php
/**
 * Created by PhpStorm.
 * User: FZS
 * Time: 2019/10/4 23:10
 */
//----------------------------------
// 十五天天气预报 调用类
//----------------------------------
class freeApi{
    private $apiUrl;

    public function __construct(){
        $this->apiUrl = 'http://t.weather.sojson.com/api/weather/city/101030100';
    }
    /**
     * 获取结果
     * @return array
     */
    public function getResult(){
        return $this->freeApiCurl($this->apiUrl);
    }
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function freeApiCurl($url,$params=false,$ispost=0){
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'free-api' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            return false;
        }
        curl_close( $ch );
        return $response;
    }
}