online advertising

How to Check if a URL is UP?

So in this small quick tutorial i am going to tell you a very short way to determine weather a website is up or not? We will be using the PHP's cURL library in order to determine so.

So lets start:


​function urlUP($url){
    //Validate the URL first!
    if(filter_var($url,FILTER_VALIDATE_URL)){
$handle curl_init(urldecode($url));
curl_setopt($handle,  CURLOPT_RETURNTRANSFERTRUE);
$response curl_exec($handle);
$httpCode curl_getinfo($handleCURLINFO_HTTP_CODE);
if($httpCode >= 200 && $httpCode 400){
return true;
}else{
return false;
}
curl_close($handle);
}
    
    }

This is the function we will be actually using, Lets break it down!

  1. We validate the URL through filter_var().
  2. Open the cURL handle for our URL.
  3. Set option in cURL to receive the incoming.
  4. Execute the handle.
  5. Get the HTTP code response for the executed handle.
  6. If HTTP Response code is greater than or equal to 200 (200 stands for Ok) or its shorter than 400, this means website is up, return true.
  7. Else return false,so that means the website is down.
  8. Close the cURL handle!
To use the function you have to do the following:


if urlUP("http://geeks-wiki.blogspot.com"){
           echo "The website is currently UP!";
}else{
           echo "The website is Down!";
}
    

Use the code freely and leave comments, additionally you can tweak the code as well, or you can also get the HTTP Codes, match them with an array of HTTP Codes and get the specific response as well!


♥ Happy Coding! ♥ 


No comments:

Post a Comment

** Comments are reviewed, but not delayed posted **