Php code for collecting weather forecasts

Source: Internet
Author: User
Tags eval explode php code string to json

Js call weather forecast method

China Meteorological Observatory

The code is as follows: Copy code
<Iframe src = "http://m.weather.com.cn/m/pn12/weather.htm" width = "245" height = "110"
Marginwidth = "0" marginheight = "0" hspace = "0" vspace = "0" frameborder = "0"
Scrolling = "no"> </iframe>

The above uses the framework method, so it is difficult to change the format style.

Simplest method

The code is as follows: Copy code

<? Php
Header ("content-type: text/html; charset = utf-8 ");
$ Weather = file_get_contents ("http://www.weather.com.cn/data/sk/101280601.html ");
Echo $ weather;
?>

Html code

The code is as follows: Copy code

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk"/>
<Style type = "text/css">
. All span {font: bold 30px/50px ""; color: red ;}
</Style>
<Title> weather forecast </title>
</Head>
<Body>
<Div class = "all">
Here is: <span class = "place"> City </span>,
The temperature is <span class = "temp"> temperature </span>,
Wind direction: <span class = "wind"> wind direction </span>,
Wind power: <span class = "windPower"> wind power </span>
</Div>
<Script type = "text/javascript" src = "http: // 127.0.0.1/jquery. js"> </script>
<Script type = "text/javascript">
$ (Function (){
$. Ajax ({
// Request address
Url: "http: // 127.0.0.1/weather. php ",
// The function executed after the request is successful
Success: function (data ){
// Parse the returned data with eval () and convert the string to JSON format
Var oD = eval (+ data + ));
// Get element with jquery-1.8.2
Var $ place = $ (". place "),
$ Temp = $ (". temp "),
$ Wind = $ (". wind "),
$ WindPower = $ (". windPower ");
// Place the returned data to the corresponding location
Using place.html (oD ["weatherinfo"] ["city"]);
$Temp.html (oD ["weatherinfo"] ["temp"] + "° ");
$Wind.html (oD ["weatherinfo"] ["WD"]);
Export windpower.html (oD ["weatherinfo"] ["WS"]);
}
});
})
</Script>
</Body>
</Html>

The above is the specified city. We can select a city on our website. For example:

The code is as follows: Copy code
<? Php
/**
* Php weather forecast code
* By www.111cn.net
*/
If (! Isset ($ _ GET ['Q']) {
// Query the location based on the IP address
$ P = file_get_contents ("http://www.jbxue.com/ip? Q = {$ _ SERVER ['http _ X_FORWARDED_FOR ']} ");
    }
Else {
$ P = $ _ GET ['Q'];
    }
$ K = 0;
$ Encoding = mb_detect_encoding () ($ p );
If ($ encoding! = "UTF-8 "){
$ P = mb_convert_encoding ($ p, "UTF-8", "gbk ");
    }
$ P_arr = array (
"01" => "Beijing ",
"02" => "Shanghai ",
"03" => "Tianjin ",
"04" => "Chongqing ",
"05" => "Heilongjiang ",
"06" => "Jilin ",
"07" => "Liaoning ",
"08" => "Inner Mongolia ",
"09" => "Hebei ",
"10" => "Shanxi ",
"11" => "Shaanxi ",
"12" => "Shandong ",
"13" => "Xinjiang ",
"14" => "Tibet ",
"15" => "Qinghai ",
"16" => "Gansu ",
"17" => "Ningxia ",
"18" => "Henan ",
"19" => "Jiangsu ",
"20" => "Hubei ",
"21" => "Zhejiang ",
"22" => "Anhui ",
"23" => "Fujian ",
"24" => "Jiangxi ",
"25" => "Hunan ",
"26" => "Guizhou ",
"27" => "Sichuan ",
"28" => "Guangdong ",
"29" => "Yunnan ",
"30" => "Guangxi ",
"31" => "Hainan ",
"32" => "Hong Kong ",
"33" => "Macao ",
"34" => "Taiwan"
);
 
Function find (& $ item, $ key, $ data ){
Global $ k;
If (preg_match ("/$ item/u", $ data )){
$ K = $ key;
        }
    }
 
Function get_data_arr ($ key ){
If (! File_exists ("./data/city {$ key}. xml ")){
$ C = file_get_contents ("http://m.weather.com.cn/data5/city?#key=.xml ");
File_put_contents ("./data/city {$ key}. xml", $ c );
        }
Else {
$ C = file_get_contents ("./data/city {$ key}. xml ");
        }
$ Arr = explode (",", $ c );
Foreach ($ arr as $ v ){
$ Data = explode ("|", $ v );
$ Ret [$ data [0] = $ data [1];
       }
Return $ ret;
    }
 
Array_walk ($ p_arr, 'Find ', $ p );
Array_walk (get_data_arr ($ k), 'Find ', $ p );
Array_walk (get_data_arr ($ k), 'Find ', $ p );
$ Ccode = get_data_arr ($ k );
Echo file_get_contents ("http://m.weather.com.cn/data/?ccode=?k=#.html ");
?>

We will not describe much of the information that can call more detailed weather information here. You can go to the weather website to see their api interfaces.

 

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.