Created
March 25, 2015 09:04
-
-
Save anonymous/5588f40d2fb11a03d06d to your computer and use it in GitHub Desktop.
composer update
This file contains 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
{ | |
"name": "root/async-calls", | |
"require": { | |
"guzzlehttp/guzzle": "~5.2" | |
} | |
} |
This file contains 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 | |
require __DIR__ . '/vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Event\CompleteEvent; | |
use GuzzleHttp\Pool; | |
// Run | |
main(); | |
/** | |
* App runner | |
* | |
* @param int $id The incremental start id | |
* @param int $chunk Pool size for making async requests | |
* @param string $fAddress File address to save found values | |
* @param string $endPoint Endpoint which the requests are executed against | |
* @param string $incrementalIdName The incremental query string param | |
* @param string $lookFor Strings that match are bowlshit! | |
*/ | |
function main( | |
$id = 50000, | |
$chunk = 1, | |
$fAddress = 'not.txt', | |
$endPoint = 'http://example.com/app/script', | |
$incrementalIdName = 'user_id', | |
$lookFor = 'Your string' | |
){ | |
$client = new Client(); $handle = fopen($fAddress, 'w'); | |
while(true) | |
{ | |
$requests = []; | |
for($i = $id; $i < ($chunk + $id); $i++) | |
{ | |
$request = $client->createRequest( | |
'GET', | |
$endPoint, | |
['timeout' => 5] | |
); | |
$request->getQuery()->set($incrementalIdName, $i); | |
$requests[] = $request; | |
} | |
$pool = new Pool($client, $requests, [ 'complete' => function(CompleteEvent $event) use(&$handle, $lookFor) | |
{ | |
$response = (string) $event->getResponse()->getBody(); | |
if(! contains($response, $lookFor)){ | |
fwrite($handle, $url = $event->getRequest()->getUrl() . "\n"); | |
echo "Found : " . ($url) . "\n"; | |
}else { | |
echo "Not found : " . $event->getRequest()->getUrl() . "\n"; | |
} | |
}]); | |
$id = $chunk + $id; | |
$pool->wait(); | |
} | |
fclose($handle); | |
} | |
/** | |
* Check that given string contains a set of values or not | |
* | |
* @param $haystack | |
* @param array $needles | |
* @return bool | |
*/ | |
function contains($haystack, $needles) | |
{ | |
foreach ((array) $needles as $needle) | |
{ | |
if ($needle != '' && strpos($haystack, $needle) !== false) return true; | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment