php function fsockopen Get SINA weather forecast

Source: Internet
Author: User
Get information on the city day of the Sina Weather Report page.
A total of two files:
Tianqi.html: Use the drop-down menu to jump to the web to get weather forecast information, return information to the floating frame;
<script>
<!--
function Go () {
var url2= ' http://php.weather.sina.com.cn/search.php?city= '
window.status= ' search in ... '; Hide Status bar Information
var A=document.form.city.selectedindex;
var City1=document.form.city.options[a].value;
Document.form.url.value=url2+city1;
Window.location=url+city1;
Document.form.submit ();
}
-
</script>
<body>
<center>
<iframe width= "height=" name= "Frame" src= "get_tianqi.php" frameborder= "1" ></iframe>
<form action= "get_tianqi.php" method= "post" name= "form" target= "Frame" >
<select name= "City" onchange= "Go ()" >
<option value= "" > Please select a city </option>
<option value= "guangzhou" > Guangzhou </option>
<option value= "Zhaoqing" > Zhaoqing </option>
<option value= "Zhanjiang" > Zhanjiang </option>
<option value= "Shenzhen" > Shenzhen </option>
<option value= "Dongguan" > Dongguan </option>
<input type= "hidden" name= "url" value= "" >
</select>
</form>
</center></body>
get_tianqi.php: Get weather information
<?php
/* This program is to get a weather forecast for a city from the Sina Web Weather page http://php.weather.sina.com.cn/search.php?city= */
Ob_start (); Start output buffering
function Request_url ($url,$method= ' POST ') {
= Parse_url (.) Parse URL address, get host,path,port,query, etc.
if (!$url) return "couldn ' t parse url";
if (!isset ($url[' Port ')) {$url[' port '] = "";}
if (!isset ($url[' query ')) {$url[' query '] = "";}
Connecting to a server
$fp = Fsockopen ($url[' host '), $url[' Port ']? $url[' Port ': 80);
if (!$FP) return "Cannot connect". $url[' host '. " Server ";
Send Request
Fputs ($FP, sprintf ("$method%s%s%s http/1.0\n", $url[' path '], $url[' query ')? "?": "", $url[' query '));
Fputs ($FP, "Host: $url[host]\n");
Fputs ($fp, "content-type:application/x-www-form-urlencoded\n");
Fputs ($fp, "connection:close\n\n");
What to return after getting the request
$line = fgets ($fp,1024);
if (!eregi ("^http/1\. ($line)) return;
$results = "";
while (!feof ($fp)) {
$line = fgets ($fp,1024);
$results. = $line;
}
Fclose ($FP);
return $results;
}
Get URLs sent from other pages (with query strings)
if (!$_request[' URL ')} {
echo "Please add a URL";
Exit
}else{
$url=$_request[' url '];
}

$content=request_url (); Web page content After the request is made

$start=strpos ($content, "<!--weather conditions Begin--");//Get the Intercept section of the weather forecast
$end=strpos ($content, "<!--weather conditions end----");
$len=$end-$start;
$b=substr ($content,$start,$len); Intercept to get the specified content
Capture the city's today's weather forecast from the Intercept section above
$sub_start=strpos ($b, "<div class=\" City_data\ ">");
$sub_end=strpos ($b, "<div class=\" weather_sm\ ">");
$len2=$sub_end-$sub_start;
$c=substr ($B,$SUB_START,$LEN2);
$d=split ("\ n", $c); Remove line break
Print_r ($d);
$e= "";
foreach ($d as $key=>$value) {
$value=strip_tags ($value); Remove HTML tags
$e.=$value;
if ($key==2) {
$e.= "<br>";
}
}
Echo $e;
Ob_end_flush ();
?>
  • 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.