public \Joomla\CMS\Http\Response
request
(mixed $method, \Joomla\Uri\UriInterface $uri, mixed $data = null, array $headers = [], mixed $timeout = null, mixed $userAgent = null)
public function request($method, UriInterface $uri, $data = null, array $headers = [], $timeout = null, $userAgent = null)
{
$options = array('method' => strtoupper($method));
if (isset($data)) {
if (is_scalar($data)) {
$options['content'] = $data;
} else {
$options['content'] = http_build_query($data);
}
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
}
$headers['Content-Length'] = \strlen($options['content']);
}
if (isset($timeout)) {
$options['timeout'] = (int) $timeout;
}
if (isset($userAgent)) {
$options['user_agent'] = $userAgent;
}
$options['ignore_errors'] = 1;
$options['follow_location'] = (int) $this->getOption('follow_location', 1);
foreach ($this->getOption('transport.stream', array()) as $key => $value) {
$options[$key] = $value;
}
$app = Factory::getApplication();
if ($app->get('proxy_enable')) {
$options['proxy'] = $app->get('proxy_host') . ':' . $app->get('proxy_port');
$options['request_fulluri'] = true;
if ($user = $app->get('proxy_user')) {
$auth = base64_encode($app->get('proxy_user') . ':' . $app->get('proxy_pass'));
$headers['Proxy-Authorization'] = 'Basic ' . $auth;
}
}
$headerEntries = array();
if (isset($headers)) {
foreach ($headers as $key => $value) {
$headerEntries[] = $key . ': ' . $value;
}
$options['header'] = implode("\r\n", $headerEntries);
}
$contextOptions = stream_context_get_options(stream_context_get_default());
$contextOptions['http'] = isset($contextOptions['http']) ? array_merge($contextOptions['http'], $options) : $options;
$context = stream_context_create(array('http' => $options, 'ssl' => array('verify_peer' => true, 'cafile' => $this->getOption('stream.certpath', CaBundle::getBundledCaBundlePath()), 'verify_depth' => 5, 'verify_peer_name' => true)));
if ($uri instanceof Uri && $this->getOption('userauth') && $this->getOption('passwordauth')) {
$uri->setUser($this->getOption('userauth'));
$uri->setPass($this->getOption('passwordauth'));
}
$php_errormsg = '';
$track_errors = ini_get('track_errors');
ini_set('track_errors', true);
$stream = @fopen((string) $uri, 'r', false, $context);
if (!$stream) {
if (!$php_errormsg) {
$php_errormsg = sprintf('Could not connect to resource: %s', $uri);
}
ini_set('track_errors', $track_errors);
throw new \RuntimeException($php_errormsg);
}
ini_set('track_errors', $track_errors);
$metadata = stream_get_meta_data($stream);
$content = stream_get_contents($stream);
fclose($stream);
if (isset($metadata['wrapper_data']['headers'])) {
$headers = $metadata['wrapper_data']['headers'];
} elseif (isset($metadata['wrapper_data'])) {
$headers = $metadata['wrapper_data'];
} else {
$headers = array();
}
return $this->getResponse($headers, $content);
}