Home > Article > Backend Development > PHP gets the weather instance code of a specified area
This article mainly introduces the relevant information of PHP to obtain the weather instance code of a specified area. Friends who need it can refer to
PHP to obtain the weather of a specified area
When developing a website, weather query is used. Since it is based on WordPress, there are many restrictions. First create a [weather.PHP] file, and then look at the code:
<?php //获取天气 $url = 'http://m.weather.com.cn/data/'; $id = '101181101'; //焦作的代号 $data = file_get_contents($url . $id .'.html'); $obj=json_decode($data); echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
For:
$url = 'http://m.weather.com.cn/data/'; $id = '101181101'; //焦作的代号 $data = file_get_contents($url . $id .'.html');
can be abbreviated as:
$data = file_get_contents('http://m.weather.com.cn/data/101181101.html');
And for:
$obj=json_decode($data);
It converts the obtained json data into an object for easy calling;
Then the last sentence:
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;
is to obtain the specified data and output it in a certain format,
$obj->weatherinfo->city //城市 $obj->weatherinfo->weather1 //今天的天气 $obj->weatherinfo->temp1 //今天的气温
Finally put
<?php include 'weather.php' ?>
where it needs to be displayed.
Thank you for reading, I hope it can help you, thank you for your support of this site!
For more PHP articles related to obtaining weather instance codes in a specified area, please pay attention to the PHP Chinese website!