Emma Stone
Emma Stone

Reputation: 135

Simple PHP ping to multiple domains

I would like to create a simple status page for all of my sites, I found this script and works fine when it comes to just pinging one site:

<?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 'not worked';
}

?>

Taken from: https://alvinalexander.com/php/php-ping-scripts-examples-ping-command

Question:

How can I check multiple sites using the script above? So to check example1.com, example2.com and so on. I am new to PHP.

Upvotes: 1

Views: 306

Answers (1)

u_mulder
u_mulder

Reputation: 54831

Check each site using loop:

$urls = ['www.google.com', '', ''];  // your sites
foreach ($urls as $url) {
    $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 'site ' . $url . ' worked';
    } else {
        echo 'site ' . $url . ' not worked';
    }
}

Upvotes: 2

Related Questions