PHP获取城市及天气
最新的版本
$url = 'http://t.weather.sojson.com/api/weather/city/101190701';
$result = Http::sendRequest($url, [], 'GET');
/**
* CURL发送Request请求,含POST和REQUEST
* @param string $url 请求的链接
* @param mixed $params 传递的参数
* @param string $method 请求的方法
* @param mixed $options CURL的参数
* @return array
*/
public static function sendRequest($url, $params = [], $method = 'POST', $options = [])
{
$method = strtoupper($method);
$protocol = substr($url, 0, 5);
$query_string = is_array($params) ? http_build_query($params) : $params;
$ch = curl_init();
$defaults = [];
if ('GET' == $method) {
$geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
$defaults[CURLOPT_URL] = $geturl;
} else {
$defaults[CURLOPT_URL] = $url;
if ($method == 'POST') {
$defaults[CURLOPT_POST] = 1;
} else {
$defaults[CURLOPT_CUSTOMREQUEST] = $method;
}
$defaults[CURLOPT_POSTFIELDS] = is_array($params) && count(array_filter($params, 'is_array')) > 0 ? $query_string : $params;
}
$defaults[CURLOPT_HEADER] = false;
$defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
$defaults[CURLOPT_FOLLOWLOCATION] = true;
$defaults[CURLOPT_RETURNTRANSFER] = true;
$defaults[CURLOPT_CONNECTTIMEOUT] = 10;
$defaults[CURLOPT_TIMEOUT] = 10;
// disable 100-continue
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
if ('https' == $protocol) {
$defaults[CURLOPT_SSL_VERIFYPEER] = false;
$defaults[CURLOPT_SSL_VERIFYHOST] = false;
}
curl_setopt_array($ch, (array)$options + $defaults);
$ret = curl_exec($ch);
$err = curl_error($ch);
if (false === $ret || !empty($err)) {
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return [
'ret' => false,
'errno' => $errno,
'msg' => $err,
'info' => $info,
];
}
curl_close($ch);
return [
'ret' => true,
'msg' => $ret,
];
}
返回示例:
forecast :今天往后天数据
yesterday:昨天的数据
{
"message": "",
"status": 200,
"date": "20250522",
"time": "2025-05-22 11:26:55",
"cityInfo": {
"city": "盐城市",
"citykey": "101190701",
"parent": "江苏",
"updateTime": "08:19"
},
"data": {
"shidu": "93%",
"pm25": 47.0,
"pm10": 59.0,
"quality": "良",
"wendu": "23.7",
"ganmao": "极少数敏感人群应减少户外活动",
"forecast": [{
"date": "22",
"high": "高温 27℃",
"low": "低温 18℃",
"ymd": "2025-05-22",
"week": "星期四",
"sunrise": "04:54",
"sunset": "18:57",
"aqi": 47,
"fx": "东北风",
"fl": "3级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}, {
"date": "23",
"high": "高温 22℃",
"low": "低温 17℃",
"ymd": "2025-05-23",
"week": "星期五",
"sunrise": "04:54",
"sunset": "18:57",
"aqi": 31,
"fx": "东北风",
"fl": "3级",
"type": "小雨",
"notice": "雨虽小,注意保暖别感冒"
}, {
"date": "24",
"high": "高温 26℃",
"low": "低温 15℃",
"ymd": "2025-05-24",
"week": "星期六",
"sunrise": "04:53",
"sunset": "18:58",
"aqi": 28,
"fx": "东北风",
"fl": "3级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}, {
"date": "25",
"high": "高温 29℃",
"low": "低温 14℃",
"ymd": "2025-05-25",
"week": "星期日",
"sunrise": "04:53",
"sunset": "18:59",
"aqi": 42,
"fx": "东南风",
"fl": "2级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
}, {
"date": "26",
"high": "高温 27℃",
"low": "低温 17℃",
"ymd": "2025-05-26",
"week": "星期一",
"sunrise": "04:52",
"sunset": "18:59",
"aqi": 46,
"fx": "东南风",
"fl": "3级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}, {
"date": "27",
"high": "高温 27℃",
"low": "低温 15℃",
"ymd": "2025-05-27",
"week": "星期二",
"sunrise": "04:52",
"sunset": "19:00",
"aqi": 40,
"fx": "东南风",
"fl": "3级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}, {
"date": "28",
"high": "高温 24℃",
"low": "低温 17℃",
"ymd": "2025-05-28",
"week": "星期三",
"sunrise": "04:52",
"sunset": "19:01",
"aqi": 35,
"fx": "东南风",
"fl": "3级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}, {
"date": "29",
"high": "高温 28℃",
"low": "低温 15℃",
"ymd": "2025-05-29",
"week": "星期四",
"sunrise": "04:51",
"sunset": "19:01",
"aqi": 40,
"fx": "东南风",
"fl": "3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
}, {
"date": "30",
"high": "高温 31℃",
"low": "低温 14℃",
"ymd": "2025-05-30",
"week": "星期五",
"sunrise": "04:51",
"sunset": "19:02",
"aqi": 42,
"fx": "东北风",
"fl": "2级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
}, {
"date": "31",
"high": "高温 35℃",
"low": "低温 17℃",
"ymd": "2025-05-31",
"week": "星期六",
"sunrise": "04:51",
"sunset": "19:03",
"aqi": 48,
"fx": "东南风",
"fl": "2级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
}, {
"date": "01",
"high": "高温 30℃",
"low": "低温 22℃",
"ymd": "2025-06-01",
"week": "星期日",
"sunrise": "04:50",
"sunset": "19:03",
"aqi": 47,
"fx": "南风",
"fl": "3级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}, {
"date": "02",
"high": "高温 24℃",
"low": "低温 20℃",
"ymd": "2025-06-02",
"week": "星期一",
"sunrise": "04:50",
"sunset": "19:04",
"aqi": 45,
"fx": "东风",
"fl": "2级",
"type": "大雨",
"notice": "出门最好穿雨衣,勿挡视线"
}, {
"date": "03",
"high": "高温 20℃",
"low": "低温 19℃",
"ymd": "2025-06-03",
"week": "星期二",
"sunrise": "04:50",
"sunset": "19:04",
"aqi": 32,
"fx": "东北风",
"fl": "2级",
"type": "中雨",
"notice": "记得随身携带雨伞哦"
}, {
"date": "04",
"high": "高温 24℃",
"low": "低温 18℃",
"ymd": "2025-06-04",
"week": "星期三",
"sunrise": "04:49",
"sunset": "19:05",
"aqi": 28,
"fx": "西北风",
"fl": "1级",
"type": "小雨",
"notice": "雨虽小,注意保暖别感冒"
}, {
"date": "05",
"high": "高温 20℃",
"low": "低温 16℃",
"ymd": "2025-06-05",
"week": "星期四",
"sunrise": "04:49",
"sunset": "19:05",
"aqi": 35,
"fx": "西南风",
"fl": "2级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
}],
"yesterday": {
"date": "21",
"high": "高温 34℃",
"low": "低温 23℃",
"ymd": "2025-05-21",
"week": "星期三",
"sunrise": "04:55",
"sunset": "18:56",
"aqi": 64,
"fx": "东南风",
"fl": "2级",
"type": "小雨",
"notice": "雨虽小,注意保暖别感冒"
}
}
}
一、获取城市
// 获取当前位置所在城市
示例:
Array(
[province] => 江苏
[city] => 盐城
[district] =>
[nowcity] => 盐城市
)
public function address() {
$getIp = $this->getClientIP();//获取真实IP
$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=2TGbi6zzFm5rjYKqPPomh9GBwcgLW5sS&ip={$getIp}&coor=bd09ll");
$json = json_decode($content);
$address = $json->{'content'}->{'address_detail'}; //按层级关系提取address数据
$return['province'] = mb_substr($address->province, 0, 2, 'utf-8');//省份
$return['city'] = mb_substr($address->city, 0, 2, 'utf-8');//城市
$return['district'] = mb_substr($address->district, 0, 2, 'utf-8');//区
return $return;
}
//获取真实IP
public function getClientIP() {
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
/* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
foreach ($arr AS $ip) {
$ip = trim($ip);
if ($ip != 'unknown') {
$realip = $ip;
break;
}
}
if (!isset($realip)) {
$realip = "0.0.0.0";
}
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
if (isset($_SERVER['REMOTE_ADDR'])) {
$realip = $_SERVER['REMOTE_ADDR'];
} else {
$realip = '0.0.0.0';
}
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$realip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP')) {
$realip = getenv('HTTP_CLIENT_IP');
} else {
$realip = getenv('REMOTE_ADDR');
}
}
preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return $realip;
}
二、获取城市天气
接口url:
1. http://www.weather.com.cn/data/sk/101010100.html
2. http://www.weather.com.cn/data/cityinfo/101010100.html
3. http://m.weather.com.cn/data/101010100.html
示例:
Array(
[weatherinfo] => Array
(
[city] => 盐城
[cityid] => 101190701
[temp1] => 16℃
[temp2] => 21℃
[weather] => 中雨
[img1] => n8.gif
[img2] => d8.gif
[ptime] => 18:00
)
)
获取天气
public function tiqnqi() {
//查询城市代号
$url = "http://www.weather.com.cn/data/cityinfo/101190701.html";
$weatherInfo_json = file_get_contents($url);
$weatherInfo = json_decode($weatherInfo_json, true);
return $weatherInfo;
}