Avevo 10 minuti liberi ed ho deciso di chiedere a ChatGPT uno script in grado di analizzare una lista di URL e di restituirmi come risultato lo status code di queste pagine.
Una sorta di URL Checker o monitor per capire se una pagina è offline o meno.

Bhè, il risultato non è niente male.
Vediamo cosa ho chiesto a ChatGPT.
NB: non ho cambiato nulla all’interno del codice.
Primo Prompt

<?php
$url_list = array("https://www.google.com", "https://www.example.com");
foreach($url_list as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 300) {
echo $url . " is working fine. HTTP status code: " . $httpcode . "<br>";
} else {
echo $url . " is not working. HTTP status code: " . $httpcode . "<br>";
$to = "alert@michelesabatini.it";
$subject = "Website down: " . $url;
$message = $url . " is not working. HTTP status code: " . $httpcode;
$headers = "From: info@example.com" . "\r\n" .
"CC: info@example.com";
mail($to, $subject, $message, $headers);
}
}
?>
Secondo prompt

<?php
$url_list = array("https://www.google.com", "https://www.example.com");
$file = 'status_codes.txt';
foreach($url_list as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 300) {
echo $url . " is working fine. HTTP status code: " . $httpcode . "<br>";
} else {
echo $url . " is not working. HTTP status code: " . $httpcode . "<br>";
$to = "alert@michelesabatini.it";
$subject = "Website down: " . $url;
$message = $url . " is not working. HTTP status code: " . $httpcode;
$headers = "From: info@example.com" . "\r\n" .
"CC: info@example.com";
mail($to, $subject, $message, $headers);
}
file_put_contents($file, $url . " - HTTP status code: " . $httpcode . "\n", FILE_APPEND | LOCK_EX);
}
?>
Terzo Prompt

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
/>
<style>
h1 {
font-family: "Montserrat", sans-serif;
text-align: center;
margin-top: 50px;
}
table {
margin: 0 auto;
width: 80%;
margin-top: 50px;
}
thead {
background-color: #333;
color: #fff;
}
th,
td {
padding: 10px;
text-align: center;
}
</style>
<title>URL Checker</title>
</head>
<body>
<h1>URL Checker</h1>
<table>
<thead>
<tr>
<th>URL</th>
<th>HTTP Status Code</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
<?php
$url_list = array("https://www.google.com", "https://www.example.com");
foreach($url_list as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "<tr>";
echo "<td>" . $url . "</td>";
echo "<td>" . $httpcode . "</td>";
echo "<td>" . date("Y-m-d H:i:s") . "</td>";
echo "</tr>";
}
?>
</tbody>
Devo dire che il codice non è scritto nel migliore dei modi, ma FUNZIONA. Certo, se abbiamo troppe URL lo script tenderà a bloccarsi o comunque impiegherà molto tempo per restituire un risultato, ma devo dire che sono soddisfatto del risultato.
Non ha utilizzato effettivamente la libreria importata. Mi sarei aspettato anche una gestione di container e col- di bootstrap, ma niente da fare.
Buona SEO,
Michele.
Lascia un commento