The code used in php to detect whether a geographical IP address is available

  • 2020-05-16 06:25:23
  • OfStack

 
/******************************************** 
* 
*  The function name: curl_string ($url,$proxy) 
*  As a   Use: test agent IP address  
*  As a   Person: li feilin  
*  day   Time: 2011-11-09 
* 
********************************************/ 
function curl_string ($url,$proxy) 
{ 
$user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh- CN; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 FirePHP/0.2.1"; 
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_PROXY, $proxy); 
curl_setopt ($ch, CURLOPT_URL, $url);// Set what to access IP 
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);// Simulate the browser the user is using  
@curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 ); //  Use automatic jump  
curl_setopt ( $ch, CURLOPT_TIMEOUT, 120 ); // Set timeout time  
curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 ); //  Automatically set Referer 

curl_setopt ($ch, CURLOPT_COOKIEJAR, 'c:\cookie.txt'); 
curl_setopt ($ch, CURLOPT_HEADER, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 10); 
$result = curl_exec($ch); 
// Check if any error occured 
if( $result === false) 
{ 
error_log(date("H:i:s") . ' Curl  failure : ' . curl_error($ch) ." -- ". $proxy."\n", 3, MYMEDIA.'/log/'.date('Y-m-d').'_Err.log'); 
}else{ 
error_log(date("H:i:s") . ' Curl  successful : ' . $proxy."\n", 3, MYMEDIA.'/log/'.date('Y-m-d').'_OK.log'); 
} 
curl_close($ch); 
return $result; 
} 

Related articles: