Skip to content

Instantly share code, notes, and snippets.

@gielfeldt
Last active August 29, 2015 14:16
Show Gist options
  • Save gielfeldt/eb660f9b179d2160c911 to your computer and use it in GitHub Desktop.
Save gielfeldt/eb660f9b179d2160c911 to your computer and use it in GitHub Desktop.
Asynchronous PHP with Background Process
<?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