1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?php
/**
* Ping an URL
*
* @param string $url The URL to ping
* @return boolean
*/
function ping_url($url) {
$return = true;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (false === curl_exec($ch)) {
$return = false;
} elseif (200 !== curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
$return = false;
}
curl_close($ch);
return $return;
}
/**
* Usage example
*/
$messages = array(
"La page <strong>%s</strong> n'existe pas.<br>",
"La page <strong>%s</strong> existe.<br>"
);
$urls = array(
'http://www.google.fr/',
'http://lululululu.net',
'http://www.dfghhhghgfh.coml',
);
foreach ($urls as $url) {
printf($messages[(int) ping_url($url)], $url);
} |
Partager