Traditional Culture Encyclopedia - Weather inquiry - How does php capture the weather forecast?

How does php capture the weather forecast?

You can use php api or preg_match_all to sneak and achieve your goal.

Here is a passage for you. I have a source code for my friends in Taiwan Province Province.

& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)

Title (\ "Content Type:? Text/html; ? charset = utf-8 \ ");

Function? getWeather($city){

$ TERM Toure? =? \"

$ city . htm \ ";

$post? =? array();

$ch? =? curl _ init();

$options? =? Array (

CURLOPT _ REFERER = & gt'',

CURLOPT _ URL = & gt$toURL,

CURLOPT _ VERBOSE = & gt0,

CURLOPT _ RETURNTRANSFER => Yes,

CURLOPT _ USERAGENT = & gt“Mozilla/4.0? (compatible; )\",

CURLOPT _ POST => Yes,

CURLOPT _ POSTFIELDS = & gthttp_build_query($post),

);

curl_setopt_array($ch,? $ options);

$ Results? =? curl _ exec($ ch); ?

curl _ close($ ch);

//Connect to central weather bureau.

Echo? & ltpre & gt';

preg _ match _ all('/& lt; Table? class=\"FcstBoxTable0 1\ "? 【^>; ]*[^>; ]* & gt; (.*)& lt; \/div & gt; /si ',$result,$matches,? PREG _ SET _ ORDER);

preg _ match _ all('/& lt; td? nowrap=\"nowrap\ "? 【^>; ]*[^>; ]* & gt; (.*)& lt; \/TD & gt; /si ',$matches[0][ 1],? $m 1,? PREG _ SET _ ORDER);

$m2? =? Explosion ('</TD >' ,$ m 1[0][ 1]);

//? print _ r($ m2); //Get daily data m2[0~6]

$ The weather? =? array();

for($ I = 0; $ i & lt=6; $i++){

preg_match_all('/src=[^>; ]*[^>; ](.*)/si ',$m2[$i],? $m5,? PREG _ SET _ ORDER); //Get the weather map file

$m6? =? explode('\ " ',$ M5[0][0]);

$wi= '

($m6[ 1],' \。 \./\.\./');

$wtitle? =? $ M6[3];

print _ r($ w title);

$weather[$i]['date']? =? Date (\"m-d\ ",? mktime(0,? 0,? 0,? Date (\"m\ "),? date(\"d\")+$i,date(\ " Y \ "));

$ Weather [$i][' temperature']? =? trim(strip _ tags($ m2[$ I]));

$weather[$i]['title']? =? $ wtitle

$weather[$i]['img']? =? $ wi

}

Return ($ weather);

}

$weather=getWeather(\ "Taipei _ city \")? ; ?

Print _ r($ weather);

//? header(\ " Location:loc . PHP \ ");

& gt first

$ TERM Toure? =? \ " http://www . cwb . gov . tw/V7/forecast/Taiwan/Inc/city/$ city . htm \ "; This is the website for reading information.

The above is central weather bureau, Taiwan Province Province.

preg _ match _ all('/& lt; Table? class=\"FcstBoxTable0 1\ "? 【^>; ]*[^>; ]* & gt; (.*)& lt; \/div & gt; /si ',$result,$matches,? PREG _ SET _ ORDER);

preg _ match _ all('/& lt; td? nowrap=\"nowrap\ "? 【^>; ]*[^>; ]* & gt; (.*)& lt; \/TD & gt; /si ',$matches[0][ 1],? $m 1,? PREG _ SET _ ORDER); The following is the webpage information table class = \ "fcstboxtable 01\" [>] * [> ]* & gt; (.*)& lt; \/div & gt; and

$m2? =? Explosion ('</TD >' ,$ m 1[0][ 1]);

//? print _ r($ m2); //Get daily data m2[0~6] Here is the daily data.

preg_match_all('/src=[^>; ]*[^>; ](.*)/si ',$m2[$i],? $m5,? PREG _ SET _ ORDER); //get the weather map file here is the map file to get the weather.

$m6? =? explode('\ " ',$ M5[0][0]);

$wi= '

($m6[ 1],' \。 \./\.\./');

$wtitle? =? $ M6[3];

print _ r($ w title);

$weather[$i]['date']? =? Date (\"m-d\ ",? mktime(0,? 0,? 0,? Date (\"m\ "),? date(\"d\")+$i,date(\ " Y \ "));

$ Weather [$i][' temperature']? =? trim(strip _ tags($ m2[$ I]));

$weather[$i]['title']? =? $ wtitle

$weather[$i]['img']? =? $ wi Here are the returned websites, dates, titles, drawings and files, etc.

$weather=getWeather(\ "Taipei _ city \")? ; ?

Print _ r($ weather); This is a week's weather forecast for this area.

Conclusion: If you want to intercept the weather forecast from the website.

In php, you can use preg_match_all (a table of a web page, the number of columns tr of the table, the field td of the table, or a wider tag div, etc.). )