The PHP file_get_contents function easily collects HTML data

  • 2020-03-31 20:35:12
  • OfStack


<?php 
//Nationwide, the criterion is whether the $REQUEST_URI contains HTML
if (!strpos($_SERVER["REQUEST_URI"],".html")) 
{ 
$page="http://qq.ip138.com/weather/"; 
$html = file_get_contents($page,'r'); 
$pattern="/<B> National major cities and counties for the same day and the next five days weather trend forecast online query </B>(.*?)<center style="padding:3px">/si"; 
//Regular matches between HTML
preg_match($pattern,$html,$pg); 
echo ""; 
//Replace the remote address with the local address
$p=preg_replace('//weather/(w+)/index.htm/', 'tq.php/$1.html', $pg[1]); 
echo $p; 
} 
//Save, the criterion is whether $REQUEST_URI contains?
else if(!strpos($_SERVER["REQUEST_URI"],"?")){ 
//Yoyo recommends the use of segmentation to obtain data. Here is the name of the obtained province
$province=explode("/",$_SERVER["REQUEST_URI"]); 
$province=explode(".",$province[count($province)-1]); 
$province=$province[0]; 
//What was commented out was my own regex, which was not good, but the effect was the same as above
//preg_match('/[^/]+[.(html)]$/',$_SERVER["REQUEST_URI"],$pro); 
//$province=preg_replace('/.html/','',$pro[0]); 
$page="http://qq.ip138.com/weather/".$province."/index.htm"; 
//Try to open the page before getting the HTML data to prevent errors caused by malicious input of the address
if (!@fopen($page, "r")) { 
die(" Sorry, the address does not exist !<a href=javascript:history.back(1)> Click here to return </a>"); 
exit(0); 
} 
$html = file_get_contents($page,'r'); 
$pattern="/ Five-day weather trend forecast </B>(.*?) Please enter the input city /si"; 
preg_match($pattern,$html,$pg); 
echo ""; 
//Regular substitution, get province, city
$p=preg_replace('//weather/(w+)/(w+).htm/', '$2.html?pro=$1', $pg[1]); 
echo $p; 
} 
else { 
//City, pass through province by get
$pro=$_REQUEST['pro']; 
$city=explode("/",$_SERVER["REQUEST_URI"]); 
$city=explode(".",$city[count($city)-1]); 
$city=$city[0]; 
//preg_match('/[^/]+[.(html)]+[?]/',$_SERVER["REQUEST_URI"],$cit); 
//$city=preg_replace('/.html?/','',$cit[0]); 
$page="http://qq.ip138.com/weather/".$pro."/".$city.".htm"; 
if (!@fopen($page, "r")) { 
die(" Sorry, the address does not exist !<a href=javascript:history.back(1)> Click here to return </a>"); 
exit(0); 
} 
$html = file_get_contents($page,'r'); 
$pattern="/ Five-day weather trend forecast </B>(.*?) Please enter the input city /si"; 
preg_match($pattern,$html,$pg); 
echo ""; 
//Get the real image address
$p=preg_replace('//image//', 'http://qq.ip138.com/image/', $pg[1]); 
echo $p; 
} 
?> 

Related articles: