PHP Gets the weather instance code for the specified region

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.