Last active
December 3, 2016 08:59
-
-
Save SuoXC/a837ae158ca905311ba65e7a3e01f668 to your computer and use it in GitHub Desktop.
fetch ip info parallelly, used for analyzing user data
This file contains hidden or 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 | |
//install dependancy: composer require guzzlehttp/guzzle | |
include 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Promise\each_limit; | |
$client = new Client(); | |
function getIps(){ | |
while(!feof(STDIN)){ | |
$ip = trim(fgets(STDIN)); | |
if($ip !== ''){ | |
yield $ip; | |
} | |
} | |
} | |
$requests = function () use ($client) { | |
foreach ( getIps() as $ip) { | |
yield $client->getAsync("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=$ip")//新浪ip api | |
->then(function ($response) use ($ip) { | |
echo "$ip\t" . $response->getBody() . "\n"; | |
}); | |
} | |
}; | |
$promise = GuzzleHttp\Promise\each_limit( | |
$requests(), | |
10 | |
); | |
$promise->wait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment