This article mainly introduces PHP to get the weather instance code of the specified area, the need for friends can refer to the following
PHP gets weather in a given area
In the development of the site to use the weather query, because it is based on WordPress so there are many restrictions, first build a "weather." PHP "File and then look at the code:
<?php //get weather $url = ' http://m.weather.com.cn/data/'; $id = ' 101181101 '; Jiaozuo's code $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 '; Jiaozuo's code $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 is to convert the obtained JSON data into an object for easy invocation;
So the last sentence:
echo $obj->weatherinfo->city. ': ' $obj->weatherinfo->weather1. ' '. $obj->weatherinfo->temp1;
is to get the specified data and output it in a certain format,
$obj->weatherinfo->city//City $obj->weatherinfo->weather1//Today's weather $obj->WEATHERINFO->TEMP1//Today's temperature
Finally, where it needs to be shown.
<?php include ' weather.php '?>
Can.
Thank you for reading, hope to help everyone, thank you for the support of this site!