Parfum Shop's Blog

You come when you need, away when you get

Check if a URL exists and return code not 404(not found) with cURL PHP

By • Mar 24th, 2010 • Category: Web Programming

Here is a simple function that will do just that, determine if a website exists using PHP and cURL. It’s check http code, if http code is 404 (Not Found) it will return false.

function urlExists($url=NULL) {
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
return true;
} else {
return false;
}
}

is
Email this author | All posts by

2 Responses »

  1. zineteregofe…

    Download mp3 with Ernest Ranglin

  2. ??? ???????? ??? ????? :)

    Here is a simple function that will do just that, determine if a website exists using PHP and cURL…..

Leave a Reply