Tamam çocuklar, önerileriniz için teşekkürler; IP 6k + var, bazı hizmetler bazı sınırlamalar nedeniyle benim istekleri başarısız olacak; Böylece, hepsini yedek modunda kullanabilirsiniz;
Aşağıdaki biçimde bir kaynak dosyamız varsa:
user_id_1 ip_1
user_id_2 ip_2
user_id_3 ip_1
Yii için bu basit ifade komutunu (PoC) kullanabilirsiniz.
class GeoIPCommand extends CConsoleCommand
{
public function actionIndex($filename = null)
{
//http://freegeoip.net/json/{$ip} //10k requests per hour
//http://ipinfo.io/{$ip}/json //1k per day
//http://ip-api.com/json/{$ip}?fields=country,city,regionName,status //150 per minute
echo "start".PHP_EOL;
$handle = fopen($filename, "r");
$destination = './good_locations.txt';
$bad = './failed_locations.txt';
$badIP = [];
$goodIP = [];
$destHandle = fopen($destination, 'a+');
$badHandle = fopen($bad, 'a+');
if ($handle)
{
while (($line = fgets($handle)) !== false)
{
$result = preg_match('#(\d+)\s+(\d+\.\d+\.\d+\.\d+)#', $line, $id_ip);
if(!$result) continue;
$id = $id_ip[1];
$ip = $id_ip[2];
$ok = false;
if(isset($badIP[$ip]))
{
fputs($badHandle, sprintf('%u %s'. PHP_EOL, $id, $ip));
continue;
}
if(isset($goodIP[$ip]))
{
fputs($destHandle, sprintf('"id":"%u","ip":"%s","from":"%s";'. PHP_EOL, $id, $ip, $goodIP[$ip]));
echo sprintf('"id":"%s","ip":"%s","from":"%s";'. PHP_EOL, $id, $ip, $goodIP[$ip]);
continue;
}
$query = @json_decode(file_get_contents('http://freegeoip.net/json/'.$ip));
$city = property_exists($query, 'region_name')? $query->region_name : '';
$city .= property_exists($query, 'city') && $query->city && ($query->city != $city) ? ', ' . $query->city : '';
if($city)
{
fputs($destHandle, sprintf('"id":"%u","ip":"%s","from":"%s";'. PHP_EOL, $id, $ip, $city));
echo sprintf('"id":"%s","ip":"%s","from":"%s";'. PHP_EOL, $id, $ip, $city);
$ok = true;
}
if(!$ok)
{
$query = @json_decode(file_get_contents('http://ip-api.com/json/'. $ip.'?fields=country,city,regionName,status'));
if($query && $query->status == 'success')
{
$city = property_exists($query, 'regionName')? $query->regionName : '';
$city .= property_exists($query, 'city') && $query->city ? ',' . $query->city : '';
if($city)
{
fputs($destHandle, sprintf('"id":"%u","ip":"%s","from":"%s";'. PHP_EOL, $id, $ip, $city));
echo sprintf('"id":"%s","ip":"%s","from":"%s";'. PHP_EOL, $id, $ip, $city);
$ok = true;
}
}
}
if(!$ok)
{
$badIP[$ip] = false;
fputs($badHandle, sprintf('%u %s'. PHP_EOL, $id, $ip));
echo sprintf('"id":"%s","ip":"%s","from":"%s";'. PHP_EOL, $id, $ip, 'Unknown');
}
if($ok)
{
$goodIP[$ip] = $city;
}
}
fclose($handle);
fclose($badHandle);
fclose($destHandle);
}else{
echo 'Can\'t open file' . PHP_EOL;
return;
}
return;
}
}
Bu bir tür boktan kod, ama işe yarıyor. kullanımı:
./yiic geoip index --filename="./source_id_ip_list.txt"
Kullanmaktan, değiştirmek ve daha iyi yapmaktan çekinmeyin)