Skip to content

Instantly share code, notes, and snippets.

@SuoXC
Last active December 3, 2016 08:59
Show Gist options
  • Save SuoXC/a837ae158ca905311ba65e7a3e01f668 to your computer and use it in GitHub Desktop.
Save SuoXC/a837ae158ca905311ba65e7a3e01f668 to your computer and use it in GitHub Desktop.
fetch ip info parallelly, used for analyzing user data
<?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