I just needed to see if a remote web server was alive, so it didn't really matter to me whether I used ping or a curl command like this.
Here's a modified version of the original source code:
<?php $url = 'www.google.com'; $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){ echo 'worked'; } else { echo "didn't work"; } ?>You can find the original source code at this “Lost in Code” URL. Issue a ping command from a PHP script
As a final option, if you can run the ping command from your command line, you can also just execute it with a PHP ping script, like this:
<?php $str = exec("ping -c 1 www.google.com"); if ($result == 0){ echo "ping succeeded"; }else{ echo "ping failed"; } ?>
In my case this didn't work because I wasn't given access to the ping command, but if you do have access to the ping command at the command line, this “PHP exec” example shows how this can work.
REF: https://alvinalexander.com/php/php-ping-scripts-examples-ping-command/