1

Is there a quicker and more efficient way to check if a website shows status codes 200 etc?

Basically I want to check specific pages whether they're online of offline. Some of the pages I check go down sporadically with 502 errors, so I need a way to also make sure those are displayed as "offline" thus using the header response check.

when using the below function on say 5-10 pages, it slows the page loading time dramatically.

<?php
function getHttpResponseCode( string $url ) //: int
{
$headers = get_headers( $url );
return substr( $headers[ 0 ], 9, 3 );
}

if (getHttpResponseCode('https://www.google.com') !='200') {
echo 'offline';
} else {
echo 'online';
}
?>

**UPDATED CODE (not working atm) **

 <?php
function checkDomain($host) {

if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
  echo 'online!';
  fclose($socket);
} else {
  echo 'offline.';
}
}
echo checkDomain('https://www.google.com');
?>

1 Answer 1

1

The speed should inprove considerably using this:

$host = 'google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
  echo 'online!';
  fclose($socket);
} else {
  echo 'offline.';
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. I will test it now and compare. How does this run with like 10 domains though? I need to copy that code for each one?
Just make a function and call that again and again
Thank you. For some reason the updated code I just added to my post is showing google as offline. Must of missed something. If you get a chance can you point me to understanding the parameters also? Just curious. Thanks again
Dang I cant figure this one out
Remove https:// from echo checkDomain('https://www.google.com');, it'll work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.