Created
December 15, 2018 18:56
-
-
Save mtmail/dc64869c9b22f3cd8850ef73a42740bc to your computer and use it in GitHub Desktop.
parallel geocoding using PHP
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
#!/usr/bin/php | |
<?php | |
// 1. install https://github.com/stil/curl-easy | |
// | |
// composer require stil/curl-easy | |
// | |
// 2. fill a text file 'queries.txt' with queries, e.g. | |
// Berlin | |
// Madrid | |
// Palermo | |
// Casablanca | |
// 24.77701,121.02189 | |
// 31.79261,35.21785 | |
// 9.54828,44.07715 | |
// 59.92903,30.32989 | |
// | |
// 3. add the APIKEY to the .php file | |
// 4. run this script | |
require 'vendor/autoload.php'; | |
$APIKEY = 'YOURAPIKEY'; | |
function generate_opencage_request_url(String $key, String $query) | |
{ | |
$base = 'https://api.opencagedata.com/geocode/v1/json'; | |
$params = array( | |
'key' => $key, | |
'query' => $query, | |
'limit' => 1, | |
'no_annotations' => 1 | |
); | |
return $base.'?'.http_build_query($params); | |
} | |
// Read file into $queries | |
$queries = []; | |
$fh = fopen('queries.txt', 'r'); | |
while (($line = fgets($fh)) !== false) { | |
array_push($queries, rtrim($line)); | |
} | |
fclose($fh); | |
// var_dump($queries); | |
// Create cURL\Request for each query | |
$requests = []; | |
foreach ($queries as $query) { | |
$request_url = generate_opencage_request_url($APIKEY, $query); | |
// print $request_url."\n"; | |
$request = new \cURL\Request($request_url); | |
array_push($requests, $request); | |
} | |
// Create queue | |
$queue = new \cURL\RequestsQueue; | |
$queue->getDefaultOptions() | |
->set(CURLOPT_TIMEOUT, 5) | |
->set(CURLOPT_RETURNTRANSFER, true); | |
$queue->addListener('complete', function (\cURL\Event $event) use (&$requests) { | |
$response = $event->response; | |
$json = $response->getContent(); | |
$data = json_decode($json, true); | |
// var_dump($data['status']); | |
if ($data['status']['code'] == 200) { | |
if (!empty($data['results'])) { | |
$result = $data['results'][0]; | |
print $result['formatted']."\n"; | |
} | |
} | |
if ($next = array_pop($requests)) { | |
$event->queue->attach($next); | |
} | |
}); | |
// three requests in parallel | |
$queue->attach(array_pop($requests)); | |
$queue->attach(array_pop($requests)); | |
$queue->attach(array_pop($requests)); | |
$queue->send(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment