Pages

Friday 17 August 2012

How to validate existing url using Curl


$url = 'http://google.com'; (suppose this is your current url)
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$page=curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// print_r(curl_getinfo($ch));die;
curl_close($ch);
if($httpcode>200) {
echo 'url exist';  //add whatever the action you want on success of url validation
}
else{
echo 'url not exist';
}

No comments:

Post a Comment