How to check website status using PHP cURL

Today we are going to see about the website status, if you are having multi domains but want to check the website status its up or down.

 

You can go with below source code, which will tell you. Now we will create simple php file which have php and html code source. Our php script will check will valid url or not using filter_var function and getting header response from curl.

<?php
// declaring varaible and values
$q = '';
$str = '';
$strFlag = " DOWN";
$intFlag = 0;

// checking the btnSubmit is set value or not
if(isset($_GET["btnSubmit"])) {

	// assigning url for q variable
	$q = $_GET["q"];

	// checking the url in curl 
	$ch = curl_init ($q);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	curl_exec ($ch);

	// getting url header response details
	$httpResponse = curl_getinfo($ch);

	// setting status code into array element
	$arrStatus = array(200,302);

	// validating url
	if(filter_var($q, FILTER_VALIDATE_URL)) {

	// comparing the http_code with array element with download content length 
		if(in_array($httpResponse["http_code"],$arrStatus) && $httpResponse["download_content_length"]>0) {
			$strFlag = " UP";
			$intFlag = 1;
		}
	}

	if($intFlag==1) {
		$str = $q." <br /> is <br /> ".$strFlag;
	}
}
?>

Now our html file which have text box and button to post the data and check the website status.

<html>
<head>
<title>Codeasearch.com - How to check website status using PHP curl</title>
</head>
<body>
<form>
<div align="center"><input type="text" name="q" size="75" value="<?php echo $q;?>" /> <input type="submit" name="btnSubmit" value="Check" /></div>
</form>
<div align="center" style="font-size:40px;background-color:#9ACD32;color:#FFFFFF;"><?php echo $str;?></div>
</body>
</html>

[ Download ] | [ Demo ]
Share