You are here

function advagg_async_send_http_request in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg.module \advagg_async_send_http_request()

Perform an HTTP request; does not wait for reply & you never will get it back.

This is a flexible and powerful HTTP client implementation. Correctly handles GET, POST, PUT or any other HTTP requests.

Parameters

$fp: (optional) A file pointer.

$request: (optional) A string containing the request headers to send to the server.

$timeout: (optional) An integer holding the stream timeout value.

Return value

bool TRUE if function worked as planed.

See also

drupal_http_request()

3 calls to advagg_async_send_http_request()
advagg_async_connect_http_request in ./advagg.module
Perform an HTTP request; does not wait for reply & you will never get it back.
advagg_install_test_async_stream in ./advagg.install
Test if STREAM_CLIENT_ASYNC_CONNECT can be used.
advagg_processor in ./advagg.module
Process variables for page.tpl.php

File

./advagg.module, line 3549
Advanced CSS/JS aggregation module

Code

function advagg_async_send_http_request($fp = NULL, $request = '', $timeout = 30) {
  static $requests = array();
  static $registered = FALSE;

  // Store data in a static, and register a shutdown function.
  $args = array(
    $fp,
    $request,
    $timeout,
  );
  if (!empty($fp)) {
    $requests[] = $args;
    if (!$registered) {
      register_shutdown_function(__FUNCTION__);
      $registered = TRUE;
    }
    return TRUE;
  }

  // Shutdown function run.
  if (empty($requests)) {
    return FALSE;
  }
  $streams = array();
  foreach ($requests as $id => $values) {
    list($fp, $request, $timeout) = $values;
    $streams[$id] = $fp;
  }
  $retry_count = 2;

  // Run the loop as long as we have a stream to write to.
  while (!empty($streams)) {

    // Set the read and write vars to the streams var.
    $read = $write = $streams;
    $except = array();

    // Do some voodoo and open all streams at once.
    $n = @stream_select($read, $write, $except, $timeout);

    // We have some streams to write to.
    if (!empty($n)) {

      // Write to each stream if it is available.
      foreach ($write as $id => $w) {
        fwrite($w, $requests[$id][1]);
        fclose($w);
        unset($streams[$id]);
      }
    }
    elseif (!empty($retry_count)) {
      $retry_count--;
    }
    else {
      break;
    }
  }

  // Free memory.
  $requests = array();
  if ($n !== FALSE && empty($streams)) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}