-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck.php
More file actions
52 lines (49 loc) · 1.29 KB
/
check.php
File metadata and controls
52 lines (49 loc) · 1.29 KB
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
45
46
47
48
49
50
51
52
<?php
require_once 'vendor/autoload.php';
$infile = $argv[1] ?? null;
$outfile = $argv[2] ?? null;
$index = $argv[3] ?? 0;
$delimiter = $argv[4] ?? ',';
if (empty($outfile)) {
die("Must specify output file. Syntax: {$argv[0]} [input csv] [output csv] [index of URL] [CSV delimiter]\n");
}
if (!file_exists($infile)) {
die("Cannot open $infile");
}
$checked = [];
if (file_exists($outfile)) {
$tmphandle = fopen($outfile, 'r');
while ($line = fgetcsv($tmphandle)) {
$checked[] = $line[0];
}
fclose($tmphandle);
}
$handle = fopen($infile, 'r');
$target = fopen($outfile, 'a');
while ($line = fgetcsv($handle, 0, $delimiter)) {
$url = trim($line[$index]);
if (empty($url)) {
// skip empty URLs.
} elseif (in_array($url, $checked)) {
echo "$url already checked; skipping...\n";
} else {
check($url, $target);
$checked[] = $url;
}
}
fclose($handle);
fclose($target);
function check($url, $target)
{
echo "Checking $url... ";
try {
$client = new \Laminas\Http\Client();
$client->setUri($url);
$response = $client->send();
$code = $response->getStatusCode();
} catch (\Exception $e) {
$code = "Exception: " . $e->getMessage();
}
echo "$code\n";
fputcsv($target, [$url, $code]);
}