Curl and Ping - How to Check Whether a Website Is Either Up or Down

curl and ping - how to check whether a website is either up or down?

something like this should work

    $url = 'yoururl';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200==$retcode) {
// All's well
} else {
// not so much
}

How can I make this online site checker work?

One of the misunderstandings of the classic if errorlevel 0 command,

which translates to (see help if)

if errorlevel is 0 or greater

which is always true for positive errorlevels.

Either

  • check if errorlevel 1 and reverse the logic

    if errorlevel 1 (GOTO :downwarning) else (GOTO :upwarning)
  • check for the current value of %errorlevel%
    if %errorlevel%==0 (GOTO :upwarning) else (GOTO :downwarning)
  • use conditional execution on success/fail &&/||

curl -i http://www.google.com/ 1> statusgoogle.txt
findstr /c:"Connection established" statusgoogle.txt &&(GOTO :upwarning)||(GOTO :downwarning)

:upwarning
echo site up
goto :eof

:downwarning
echo site down

Checking if a website exists

You could simply use HEAD request to get only the headers of the page and not the whole page. Still, the website will still generate the full page but at least you won't download everything.

To achieve this, you can use many methods, just check how to change the headers of the request and instead of doing a GET, you can do a HEAD.

Fastest way to ping thousands of websites using PHP

You can either fork your php process using pcntl_fork or look into curl's built-in multi-threading. https://web.archive.org/web/20091014034235/http://www.ibuildings.co.uk/blog/archives/811-Multithreading-in-PHP-with-CURL.html



Related Topics



Leave a reply



Submit