You are here

function background_process_http_request in Background Process 7.2

Same name and namespace in other branches
  1. 8 background_process.module \background_process_http_request()
  2. 6 background_process.module \background_process_http_request()
  3. 7 background_process.module \background_process_http_request()

Perform an http request.

See also

drupal_http_request()

2 calls to background_process_http_request()
background_process_dispatcher_http in ./background_process.dispatchers.inc
Dispatcher for HTTP
background_process_http_request_get_response in ./background_process.http.inc
Get response for an http request

File

./background_process.http.inc, line 62
This contains the HTTP functions for Background Process.

Code

function background_process_http_request($url, array $options = array()) {

  // Parse the URL and make sure we can handle the schema.
  $result = new stdClass();
  $result->url = $url;
  $result->options = $options;
  $uri = @parse_url($url);
  $result->uri = $uri;
  if ($uri == FALSE) {
    $result->error = 'unable to parse URL';
    $result->code = -1001;
    return _background_process_http_request_result($result);
  }
  if (!isset($uri['scheme'])) {
    $result->error = 'missing schema';
    $result->code = -1002;
    return _background_process_http_request_result($result);
  }

  // Merge the default options.
  $options += array(
    'headers' => array(),
    'method' => 'GET',
    'data' => NULL,
    'max_redirects' => 3,
    'timeout' => variable_get('background_process_connection_timeout', BACKGROUND_PROCESS_CONNECTION_TIMEOUT),
    'context' => NULL,
    'blocking' => FALSE,
    'postpone' => FALSE,
  );

  // stream_socket_client() requires timeout to be a float.
  $options['timeout'] = (double) $options['timeout'];
  $host = NULL;
  switch ($uri['scheme']) {
    case 'http':
    case 'feed':
      $port = isset($uri['port']) ? $uri['port'] : 80;
      $socket = 'tcp://' . $uri['host'] . ':' . $port;

      // RFC 2616: "non-standard ports MUST, default ports MAY be included".
      // We don't add the standard port to prevent from breaking rewrite rules
      // checking the host that do not take into account the port number.
      $host = $uri['host'] . ($port != 80 ? ':' . $port : '');
      break;
    case 'https':

      // Note: Only works when PHP is compiled with OpenSSL support.
      $port = isset($uri['port']) ? $uri['port'] : 443;
      $socket = 'ssl://' . $uri['host'] . ':' . $port;
      $host = $uri['host'] . ($port != 443 ? ':' . $port : '');
      break;
    default:
      $result->error = 'invalid schema ' . $uri['scheme'];
      $result->code = -1003;
      return _background_process_http_request_result($result);
  }
  if (!empty($host) && empty($options['headers']['Host'])) {
    $options['headers']['Host'] = $host;
  }
  $result->options = $options;
  $result->socket = $socket;
  $result->postponed = $options['postpone'];
  if ($result->postponed) {
    return $result;
  }
  else {
    return background_process_http_request_initiate($result);
  }
}