Reputation: 135
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
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