Spiga

PHP : Using CURL to validate URL address

About CURL

cURL stands for client URLs (and is also written as just curl or Curl),is a commandline tool for working with URLs.. Withg cURL you can access Web sites,FTP files, and do much,much more.PHP can use cURL via the shell_exec() and other system functions. But PHP also supports libcurl, a cURL library.

Code:
Below code will validate whether given URL(http://www.suresh-mobileweb.blogspot.com) is valid using cURL.

<?php

$ch = curl_init(); //initialize curl session
curl_setopt ($ch, CURLOPT_URL, "http://www.suresh-mobileweb.blogspot.com"); //connect to URL
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); //return response as srting
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0); //unlimited time connection
curl_exec($ch); //execute the session
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE); //get URL http response

if($httpcode != 200){ // the URL is not valid - print the result
echo "invalid Site URL";
}else{ //valid
echo "valid Site URL";
}

curl_close($ch); //close the curl session
?>

0 comments: