高德API接口获取天气预报
2021-03-24 21:25:10
高德天气API接口地址:https://developer.amap.com/api/webservice/guide/api/weatherinfo
function get_client_ip() {
$ip = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] AS $xip) {
if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
$ip = $xip;
break;
}
}
}
return $ip;
}
function file_get_contents_by_curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$result = curl_exec($ch);
curl_close($ch);return $result;
}
$clientip = get_client_ip();
$location = file_get_contents_by_curl('https://restapi.amap.com/v3/ip?ip='.$clientip.'&output=json&key=高德API的KEY');
$obj_location = json_decode($location);
// var_dump($location);
$province = $obj_location->province;
$cityname = $obj_location->city;
$citycode = $obj_location->adcode;
$weather = file_get_contents_by_curl('https://restapi.amap.com/v3/weather/weatherInfo?city='.$citycode.'&key=高德API的KEY&output=json&extensions=all');
$weather = json_decode($weather,true);
$weather = $weather['forecasts'][0]['casts'];//4天天气预报
foreach ($weather as $key => $value) {
echo $value['date'].'<br>';
echo $value['daytemp'].'<br>';
echo $value['nighttemp'].'<br>';
}
var_dump($weather);
exit;
function get_client_ip() {
$ip = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] AS $xip) {
if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
$ip = $xip;
break;
}
}
}
return $ip;
}
function file_get_contents_by_curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$result = curl_exec($ch);
curl_close($ch);return $result;
}
$clientip = get_client_ip();
$location = file_get_contents_by_curl('https://restapi.amap.com/v3/ip?ip='.$clientip.'&output=json&key=高德API的KEY');
$obj_location = json_decode($location);
// var_dump($location);
$province = $obj_location->province;
$cityname = $obj_location->city;
$citycode = $obj_location->adcode;
$weather = file_get_contents_by_curl('https://restapi.amap.com/v3/weather/weatherInfo?city='.$citycode.'&key=高德API的KEY&output=json&extensions=all');
$weather = json_decode($weather,true);
$weather = $weather['forecasts'][0]['casts'];//4天天气预报
foreach ($weather as $key => $value) {
echo $value['date'].'<br>';
echo $value['daytemp'].'<br>';
echo $value['nighttemp'].'<br>';
}
var_dump($weather);
exit;