J'ai un script qui vérifie un site Web ($ host) pour une chaîne de caractères ($ find). Si la chaîne existe, rien ne se passe, si la chaîne n'est pas trouvée, un e-mail est envoyé à une adresse e-mail prédéfinie. Le problème que j'ai est que j'ai besoin d'un tableau d'URL et je crois un deuxième tableau de texte. Le texte du tableau doit correspondre aux URL du tableau. Peut-être stocker les URL et le texte dans un fichier (s) texte pourrait être une meilleure solution.Ajout d'un tableau à un script php
Voici le script tel qu'il est actuellement, travaillant sur le domaine unique.
<?php
$host = 'www.my-domain.com';
$find = 'content on my page';
function check($host, $find) {
$fp = fsockopen($host, 80, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
$header = "GET/HTTP/1.1\r\n";
$header .= "Host: $host\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($fp, $header);
while (!feof($fp)) {
$str.= fgets($fp, 1024);
}
fclose($fp);
return (strpos($str, $find) !== false);
}
}
function alert($host) {
mail('[email protected]', 'Monitoring', $host.' down');
}
if (!check($host, $find)) alert($host);
?>
Nouveau code avec tableau en place:
$hostMap = array(
'www.my-domain.com' => 'content on site',
'www.my-domain2.ca' => 'content on second site',
);
foreach ($hostMap as $host => $find)
{
function check($host, $find)
{
$fp = fsockopen($host, 80, $errno, $errstr, 10);
if (!$fp)
{
echo "$errstr ($errno)\n";
} else {
$header = "GET/HTTP/1.1\r\n";
$header .= "Host: $host\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($fp, $header);
while (!feof($fp)) {
$str.= fgets($fp, 1024);
}
fclose($fp);
return (strpos($str, $find) !== false);
}
}
function alert($host)
{
mail('[email protected]', 'Website Monitoring', $host.' is down');
}
print $host;
print $find;
//if (!check($host, $find)) alert($host);
if(!check($host, $find))
{
alert($host);
}
}
?>
Déplacé les fonctions en dehors du foreach (
ini_set('display_errors', true);
$hostMap = array(
'www.my-domain.com' => 'content on site',
'www.my-domain2.ca' => 'content on second site',
);
function check($host, $find)
{
$fp = fsockopen($host, 80, $errno, $errstr, 10);
if (!$fp)
{
echo "$errstr ($errno)\n";
} else {
$header = "GET/HTTP/1.1\r\n";
$header .= "Host: $host\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($fp, $header);
while (!feof($fp)) {
$str.= fgets($fp, 1024);
}
fclose($fp);
return (strpos($str, $find) !== false);
}
}
function alert($host)
{
mail('[email protected]', 'Website Monitoring', $host.' is down');
}
print $host;
print $find;
//if (!check($host, $find)) alert($host);
foreach ($hostMap as $host => $find)
{
if(!check($host, $find))
{
alert($host);
}
}
?>
Voici le code final avec un tableau de travail au cas où quelqu'un d'autre veut une solution comme celle-ci:
function check($host, $find)
{
$fp = fsockopen($host, 80, $errno, $errstr, 10);
if (!$fp)
{
echo "$errstr ($errno)\n";
} else {
$header = "GET/HTTP/1.1\r\n";
$header .= "Host: $host\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($fp, $header);
while (!feof($fp)) {
$str.= fgets($fp, 1024);
}
fclose($fp);
return (strpos($str, $find) !== false);
}
}
function alert($host)
{
$headers = 'From: Set your from address here';
mail('[email protected]', 'Website Monitoring', $host.' is down' $headers);
}
$hostMap = array(
'www.my-domain.com' => 'content on site',
'www.my-domain2.com' => 'content on second site',
);
//if (!check($host, $find)) alert($host);
foreach ($hostMap as $host => $find)
{
if(!check($host, $find))
{
alert($host);
}
}
unset($host);
unset($find);
?>
La troisième itération du code a un foreach qui ne semble pas fonctionner dans le tableau. Le premier site du tableau est le seul vérifié. Le seul message que je reçois est "Undefined variable: str in /srv/www/php/php_testing/monitor.php on line 25". Je ne sais pas où est mon erreur. – Havock