Have the option to run a function every time in the event loop. Might be useful if you have 20 requests and you only need 4 of them, and whatever other ones come (of the 16 left) in while getting those 4 is nice but not needed. This would allow you get the required stuff while skipping the unnecessary things that are taking too long to execute.


mikeytown2’s picture

Title:Run function every time in the event loop» Option to run function at the end of httprl_post_processing that can alter the $responses and $streams variables

I think a better way to accomplish this goal is to have a function that can run at the end of httprl_post_processing() that can alter the $responses and $streams variables in httprl_send_request().

mikeytown2’s picture

Status:Active» Fixed
new6.68 KB

Test code for this

// Array of URLs to get.
$urls = array(

// Process list of URLs.
$options = array(
  'alter_all_streams_function' => 'need_two_good_results',
  'callback' => array(array('function' => 'limit_data_size')),
// Queue up the requests.
httprl_request($urls, $options);

// Execute requests.
$requests = httprl_send_request();

// Print what was done.
echo httprl_pr($requests);

function need_two_good_results($id, &$responses, &$streams, $current_time) {
  static $counter = 0;
  foreach ($responses as $id => &$result) {
    // Skip if we got a 200.
    if ($result->code == 200) {
      $counter = $counter + 1;

    if ($counter >= 2) {
      // Set the code to request was aborted.
      $result->code = HTTPRL_REQUEST_ABORTED;
      $result->status = 'Done.';
      $result->options['timeout'] = $result->options['timeout'] - $current_time;

      // Close the file pointer and remove from the stream from the array.

function limit_data_size(&$result) {
  // Only use the first and last 256 characters in the data array.
  $result->data = substr($result->data, 0, 256) . "\n\n ... \n\n" . substr($result->data, strlen($result->data)-256);

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.