Created
November 19, 2021 11:22
-
-
Save MiniCodeMonkey/65b36941a3c3d0ef9008f11c98c511ed to your computer and use it in GitHub Desktop.
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 | |
require('vendor/autoload.php'); | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Exception\RequestException; | |
use GuzzleHttp\Pool; | |
use GuzzleHttp\Psr7\Request; | |
use GuzzleHttp\Psr7\Response; | |
$batchSize = 2000; // How many addreses to process per batch geocoding API request | |
$concurrency = 4; // How many batches to process concurrently | |
$apiKey = 'DEMO'; | |
function buildSampleDataset($addressesCount = 10000) { | |
return array_fill(0, $addressesCount, 'Washington DC'); | |
} | |
function buildRequests($chunks) { | |
global $client, $apiKey; | |
foreach ($chunks as $chunk) { | |
yield function() use ($client, $apiKey, $chunk) { | |
return $client->postAsync('', [ | |
'query' => [ | |
'api_key' => $apiKey, | |
'limit' => 1 | |
], | |
'json' => $chunk | |
]); | |
}; | |
} | |
}; | |
$client = new Client([ | |
'base_uri' => 'https://api.geocod.io/v1.7/geocode', | |
'connect_timeout' => 10, | |
'timeout' => 60 * 30, | |
]); | |
$addresses = buildSampleDataset(); | |
$chunks = array_chunk($addresses, $batchSize); | |
echo 'Processing ' . count($addresses) . ' addresses in ' . count($chunks) . ' separate batches'; | |
$requests = buildRequests($chunks); | |
$pool = new Pool($client, $requests, [ | |
'concurrency' => $concurrency, | |
'fulfilled' => function (Response $response, $index) { | |
echo 'Got response for request #' . $index . ': ' . substr((string)$response->getBody(), 0, 250) . PHP_EOL; | |
// this is delivered each successful response | |
}, | |
'rejected' => function (RequestException $reason, $index) { | |
echo 'Request #' . $index . ' failed' . PHP_EOL; | |
// this is delivered each failed request | |
}, | |
]); | |
// Initiate the transfers and create a promise | |
$promise = $pool->promise(); | |
// Force the pool of requests to complete. | |
$promise->wait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment