Php ajax allows you to get the weather status without refreshing

Source: Internet
Author: User

First, we need to understand that we cannot do the weather forecast function by ourselves. Here we only need to call the data returned by the api, the following is an example of how to call the api of www.weather.com.cn. Weather has become an indispensable topic in our life and is closely related to our life. On the right side of my blog, I used php + ajax to create a weather query module.

The ideal status should be that the user automatically obtains local weather information based on different access locations, but the current technology is limited, and only manual query can be completed. This is much simpler, and too many technologies are not used. The main reason is that ajax is used to call an open interface and then process the returned json data.

Interface address: http://www.weather.com.cn/data/cityinfo/101200101.html

Returned value: {"weatherinfo": {"city": "Wuhan", "cityid": "101200101", "temp1": "28 ℃", "temp2 ": "36 ℃", "weather": "clear to cloudy", "img1": "n0.gif", "img2": "d1.gif", "ptime "}}


The interface address part is "101200101". The ID string is the city id. The ID corresponding to my Baidu city is encapsulated into an array and can be called directly when used. There are not many core code, mainly because the city -- the ID is relatively large, so I will not post the source code, just package it and share it directly. A friend in need can download it directly!

Some code

The Code is as follows: Copy code


<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script type = "text/javascript" src = "jquery. js"> </script>
<Script type = "text/javascript">
$ (Function (){

$ ("# Submit"). click (function (){
// Send an ajax request
Var city = $ ("# city"). val ();
$. Post ("getweather. php", {city: city}, function (data ){
If (data. weatherinfo. city ){
Var city = data. weatherinfo. city; // city name
Var temp1 = data. weatherinfo. temp1; // maximum temperature
Var temp2 = data. weatherinfo. temp2; // minimum temperature
Var weather = data. weatherinfo. weather; // weather description ("clear to cloudy ")
Alert (city + ":" + weather + "," + temp2 + "-" + temp1 );
Return;
} Else {
Alert ("the city is not found ");
}
}, "Json ");
});

});
</Script>

Getweather. php file

The Code is as follows: Copy code

<Form method = "post">
Enter the city: <input type = "text" name = "city" id = "city" value = "Wuhan"/>
<Input type = "button" name = "sub" id = "submit" value = "view weather"/>
</Form>


Is, <? Php

Include "citycode. php ";
$ City = $ _ POST ['city'];
$ Citycode = @ $ citycode [$ city];
// Echo "shibushi ";
If (empty ($ citycode )){
Echo "the city you entered is out of the range ";
} Else {
Echo file_get_contents ("http://www.weather.com.cn/data/cityinfo/". $ citycode. ". html ");
}
?>

Test Results

Source code download: php ajax implements refreshing to get the weather status Source Code download:

 

Related Article

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.