-
-
Save napoler/4bb4ac24e3f3e559ca2d to your computer and use it in GitHub Desktop.
Asynchronous PHP with Background Process
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 | |
// Fetch main feed. | |
// Main feed at /items.json | |
// Individual item feeds at /item/<id>.json | |
$url = 'http://example.com/items.json'; | |
$result = drupal_http_request($url); | |
$data = drupal_json_decode($result->data); | |
// Subfetch subitems for each json item in main json feed. | |
// Subitems only available via single item fetch. | |
$processes = array(); | |
foreach ($data['response'] as &$response) { | |
// Generate URL and headers | |
$url = 'http://example.com/item/' . $response['id'] . '.json'; | |
// Postpone request polling and set callback. | |
$options = array( | |
'postpone' => TRUE, | |
'callback' => function ($result) use (&$response) { | |
$item = drupal_json_decode($result->data); | |
$response['subitems'] = $item['response']['subitems']; | |
} | |
); | |
// Add request object to our polling pool. | |
$processes[] = background_process_http_request($url, $options); | |
} | |
// Wait for processes to finish, max 10 concurrent requests at a time. | |
background_process_http_request_process($processes, array('limit' => 10)); | |
// $data is now populated. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment