function background_process_http_request in Background Process 8
Same name and namespace in other branches
- 6 background_process.module \background_process_http_request()
- 7.2 background_process.http.inc \background_process_http_request()
- 7 background_process.module \background_process_http_request()
Implements to Perform an http request.
3 calls to background_process_http_request()
- BackgroundProcess::dispatch in ./
background_process.class.php - Implements to Dispatch Process.
- background_process_determine_default_service_host in ./
background_process.module - Implements to Determine host for current installation.
- background_process_http_request_get_response in ./
background_process.module - Get response for an http request.
File
- ./
background_process.module, line 879 - This module implements a framework for calling funtions in the background.
Code
function background_process_http_request($url, array $options = []) {
// Parse the URL and make sure we can handle the schema.
$result = new stdClass();
$result->url = $url;
$result->options = $options;
$result->code = NULL;
$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);
}
// Set default context to enable/disable SSL verification.
$default_context = stream_context_create([
'ssl' => [
'verify_peer' => \Drupal::config('background_process.settings')
->get('background_process_ssl_verification'),
'verify_peer_name' => \Drupal::config('background_process.settings')
->get('background_process_ssl_verification'),
],
]);
// Merge the default options.
$options += [
'headers' => [],
'method' => 'GET',
'data' => NULL,
'max_redirects' => 3,
'timeout' => \Drupal::config('background_process.settings')
->get('background_process_connection_timeout'),
'context' => $default_context,
'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;
// 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);
}
}