Skip to content

Instantly share code, notes, and snippets.

Created March 25, 2015 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5588f40d2fb11a03d06d to your computer and use it in GitHub Desktop.
Save anonymous/5588f40d2fb11a03d06d to your computer and use it in GitHub Desktop.
composer update
{
"name": "root/async-calls",
"require": {
"guzzlehttp/guzzle": "~5.2"
}
}
<?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