Last active
December 21, 2023 09:05
-
-
Save ericewers/c2c6ed0aa0963d8a401680837ca8cc92 to your computer and use it in GitHub Desktop.
HTTP Response Codes and URL Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get HTTP Response Codes | |
function getHTTPCode($httpcode) { | |
switch ($httpcode) { | |
case 100: $text = '100 Continue'; break; | |
case 101: $text = '101 Switching Protocols'; break; | |
case 200: $text = '200 OK'; break; | |
case 201: $text = '201 Created'; break; | |
case 202: $text = '202 Accepted'; break; | |
case 203: $text = '203 Non-Authoritative Information'; break; | |
case 204: $text = '204 No Content'; break; | |
case 205: $text = '205 Reset Content'; break; | |
case 206: $text = '206 Partial Content'; break; | |
case 300: $text = '300 Multiple Choices'; break; | |
case 301: $text = '301 Moved Permanently'; break; | |
case 302: $text = '302 Moved Temporarily'; break; | |
case 303: $text = '303 See Other'; break; | |
case 304: $text = '304 Not Modified'; break; | |
case 305: $text = '305 Use Proxy'; break; | |
case 400: $text = '400 Bad Request'; break; | |
case 401: $text = '401 Unauthorized'; break; | |
case 402: $text = '402 Payment Required'; break; | |
case 403: $text = '403 Forbidden'; break; | |
case 404: $text = '404 Not Found'; break; | |
case 405: $text = '405 Method Not Allowed'; break; | |
case 406: $text = '406 Not Acceptable'; break; | |
case 407: $text = '407 Proxy Authentication Required'; break; | |
case 408: $text = '408 Request Time-out'; break; | |
case 409: $text = '409 Conflict'; break; | |
case 410: $text = '410 Gone'; break; | |
case 411: $text = '411 Length Required'; break; | |
case 412: $text = '412 Precondition Failed'; break; | |
case 413: $text = '413 Request Entity Too Large'; break; | |
case 414: $text = '414 Request-URI Too Large'; break; | |
case 415: $text = '415 Unsupported Media Type'; break; | |
case 500: $text = '500 Internal Server Error'; break; | |
case 501: $text = '501 Not Implemented'; break; | |
case 502: $text = '502 Bad Gateway'; break; | |
case 503: $text = '503 Service Unavailable'; break; | |
case 504: $text = '504 Gateway Time-out'; break; | |
case 505: $text = '505 HTTP Version not supported'; break; | |
default: | |
$text = ''; | |
break; | |
} | |
return $text; | |
} | |
// Format File Size | |
function formatBytes($bytes, $precision = 2) { | |
$units = array('B', 'KB', 'MB', 'GB', 'TB'); | |
$pow = floor(log($bytes) / log(1024)); | |
$bytes /= pow(1024, $pow); | |
return round($bytes, $precision) . ' ' . $units[$pow]; | |
} | |
// Get Results | |
function getResult($urls) { | |
$ch_array = array(); | |
foreach($urls as $url) { | |
$ch = curl_init($url); | |
array_push($ch_array, $ch); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_NOBODY, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
} | |
$mh = curl_multi_init(); | |
foreach($ch_array as $ch) { | |
curl_multi_add_handle($mh, $ch); | |
} | |
$running = null; | |
do { | |
curl_multi_exec($mh, $running); | |
} while ($running); | |
foreach($ch_array as $ch) { | |
curl_multi_remove_handle($mh, $ch); | |
} | |
curl_multi_close($mh); | |
$response_array = array(); | |
foreach($ch_array as $ch) { | |
array_push($response_array, curl_multi_getcontent($ch)); | |
} | |
$output = array(); | |
foreach($response_array as $key => $response) { | |
// Debugging | |
//echo "<pre>" . var_dump($response) . "</pre>"; | |
if (preg_match('/(Location: )(?:https?:\/\/)(?:[^?\/\s]+[?\/])(.*)/', $response, $matches)) { | |
$url = substr($matches[0], 10); | |
} else { | |
$url = $urls[$key]; | |
} | |
if (preg_match('/Content-Length: (\d+)/', $response, $matches)) { | |
if ( (int)$matches[1] == 0) { | |
$fileSize = "N/A"; | |
} else { | |
$fileSize = formatBytes((int)$matches[1]); | |
} | |
} else { | |
$fileSize = "N/A"; | |
} | |
$response = explode(" ", $response); | |
$httpcode = (int)$response[1]; | |
$content = "URL: " . $url . "<br>" . | |
"File Size: " . $fileSize . "<br>" . | |
"Response: " . getHTTPCode($httpcode) . "<br><br>"; | |
array_push($output, $content); | |
} | |
curl_close($ch); | |
return $output; | |
} | |
if($_POST) { | |
$urls = explode(PHP_EOL, trim(str_replace("\r", "", $_POST['URLS']))); | |
$results = getResult($urls); | |
} | |
?> | |
<html> | |
<head> | |
<title>URL Checker</title> | |
<style> | |
textarea, | |
button { | |
display: block; | |
margin: 30px; | |
padding: 10px; | |
} | |
textarea { | |
width: 1000px; | |
height: 300px; | |
} | |
</style> | |
</head> | |
<body> | |
<form method="POST"> | |
<textarea type="text" name="URLS" required></textarea> | |
<button type="submit" name="submit">Check URLS</button> | |
</form> | |
<?php | |
if($_POST) { | |
foreach($results as $result) { | |
echo $result; | |
} | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment