Home >Backend Development >PHP Tutorial >PHP function fsockopen gets Sina weather forecast

PHP function fsockopen gets Sina weather forecast

巴扎黑
巴扎黑Original
2016-11-24 14:48:401141browse

Get the city's current day information from the Sina weather forecast webpage.
There are two files in total:
tianqi.html: Use the drop-down menu to jump to the webpage to get the weather forecast information, and return the information to the floating frame;


<script> <br><!-- <br/>function go(){ <br/>var url2='http://php.weather.sina.com.cn/search.php?city=' <br/> window.status='Searching...'; //Hide status bar information <br/>var a=document.form.city.selectedIndex; <br/>var city1=document.form.city.options[a].value; <br/>document. form.url.value=url2+city1; <br/>//window.location=url+city1; <br/>document.form.submit(); <br/>} <br/>--> <br></script>

< ;center>







get_tianqi.php: Get weather forecast information
/* This program is to obtain the weather forecast of a certain city from Sina’s weather page http://php.weather.sina.com.cn/search.php?city=*/
ob_start() ; //Start output buffering
function request_url($url,$method='POST') {
$url = parse_url($url); //Parse the url address and obtain host, path, port, query, etc.
if (! $url) return "couldn't parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url[' query'])) { $url['query'] = ""; }
//Connect to the 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.0n", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host : $url[host]n");
fputs($fp, "Content-type: application/x-www-form-urlencodedn");
fputs($fp, "Connection: closenn");
//Get Content returned after request
$line = fgets($fp,1024);
if (!eregi("^HTTP/1.. 200", $line)) return;
$results = "";
while(! feof($fp)) {
$line = fgets($fp,1024);
$results .= $line;
}
fclose($fp);
return $results;
}
//Get from other web pages The sent url (with query string)
if (!$_REQUEST['url']) {
echo "Please add a url";
exit;
}else{
$url=$_REQUEST['url'] ;
}

$content=request_url($url); //Get the requested web page content

$start=strpos($content,"");// Get the intercepted section of the weather forecast
$end=strpos($content,"");
$len=$end-$start;
$b=substr($content, $start,$len); //Get the specified content
//Get the interception segment of today’s weather forecast for the city from the above interception segment
$sub_start=strpos($b,"
");
$sub_end=strpos($b,"
");
$len2=$sub_end-$sub_start;
$c=substr($b,$sub_start,$len2 ; ){
$value=strip_tags($value); //Remove html tag
$e.=$value; 
if ($key==2) { 
   $e.="
"; 


echo $e; 
ob_end_flush(); 
?>  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn