You are here

function httprl_background_processing in HTTP Parallel Request & Threading Library 7

Same name and namespace in other branches
  1. 6 httprl.module \httprl_background_processing()

Output text, close connection, continue processing in the background.

Parameters

string $output: Text to output to open connection.

bool $wait: Wait 1 second?

string $content_type: Content type header.

int $length: Content length.

Return value

bool Returns TRUE if operation worked, FALSE if it failed.

2 calls to httprl_background_processing()
httprl.examples.php in examples/httprl.examples.php
HTTP Parallel Request Library code examples.
httprl_async_page in ./httprl.async.inc
Menu Callback; run given function.

File

./httprl.module, line 2635
HTTP Parallel Request Library module.

Code

function httprl_background_processing($output, $wait = TRUE, $content_type = "text/html; charset=utf-8", $length = 0) {

  // Can't do background processing if headers are already sent.
  if (headers_sent()) {
    return FALSE;
  }

  // Prime php for background operations.
  // Remove any output buffers.
  @ob_end_clean();
  $loop = 0;
  while (ob_get_level() && $loop < 25) {
    @ob_end_clean();
    $loop++;
  }

  // Ignore user aborts.
  ignore_user_abort(TRUE);

  // Output headers & data.
  ob_start();
  header("HTTP/1.0 200 OK");
  header("Content-type: " . $content_type);
  header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
  header("Cache-Control: no-cache");
  header("Cache-Control: must-revalidate");
  header("Connection: close");
  header('Etag: "' . microtime(TRUE) . '"');
  print $output;
  $size = ob_get_length();
  header("Content-Length: " . $size);
  @ob_end_flush();
  @ob_flush();
  @flush();
  if (function_exists('fastcgi_finish_request')) {
    fastcgi_finish_request();
  }

  // Wait for 1 second.
  if ($wait) {
    sleep(1);
  }

  // Text returned and connection closed.
  // Do background processing. Time taken after should not effect page load times.
  return TRUE;
}