Skip to content

Instantly share code, notes, and snippets.

@kyletaylored
Created April 12, 2022 05:25
Show Gist options
  • Select an option

  • Save kyletaylored/67a8564017da5f676d09cbf68c60549a to your computer and use it in GitHub Desktop.

Select an option

Save kyletaylored/67a8564017da5f676d09cbf68c60549a to your computer and use it in GitHub Desktop.
<?php
$myFile = new SplFileObject("data.txt");
$ips = [];
$count=0;
while (!$myFile->eof()) {
$ip = trim($myFile->fgets());
// Skip if IPv6
if(stripos($ip, ':') !== false) {
continue;
}
$ip_data = json_decode(file_get_contents('https://maxmind.pantheon.support/asn/'.$ip), true);
$geo = json_decode(file_get_contents('https://maxmind.pantheon.support/city/'.$ip));
$max = array_merge($ip_data, [
'continent' => $geo->continent->names->en,
'country' => $geo->country->names->en,
'state' => $geo->subdivisions[0]->names->en,
'city' => $geo->city->names->en,
'latlon' => $geo->location->latitude . ", " . $geo->location->longitude
]);
$ips[] = $max;
$count++;
echo "Count: ${count}" . PHP_EOL;
}
$f = fopen('mycsv.csv', 'w+'); // Configure fopen to create, open, and write data.
fputcsv($f, array_keys($ips[0])); // Add the keys as the column headers
// Loop over the array and passing in the values only.
foreach ($ips as $row) {
fputcsv($f, array_values($row));
}
// Close the file
fclose($f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment