function request($url, array $options = NULL)
{
$fw = Base::instance();
$parts = parse_url($url);
if (empty($parts['scheme'])) {
// Local URL
$url = $fw->get('SCHEME') . '://' . $fw->get('HOST') . ($url[0] != '/' ? $fw->get('BASE') . '/' : '') . $url;
$parts = parse_url($url);
} elseif (!preg_match('/https?/', $parts['scheme'])) {
return FALSE;
}
if (!is_array($options)) {
$options = [];
}
if (empty($options['header'])) {
$options['header'] = [];
} elseif (is_string($options['header'])) {
$options['header'] = [$options['header']];
}
if (!$this->wrapper) {
$this->engine();
}
if ($this->wrapper != 'stream') {
// PHP streams can't cope with redirects when Host header is set
foreach ($options['header'] as &$header) {
if (preg_match('/^Host:/', $header)) {
$header = 'Host: ' . $parts['host'];
unset($header);
break;
}
}
$this->subst($options['header'], 'Host: ' . $parts['host']);
}
$this->subst($options['header'], ['Accept-Encoding: gzip,deflate', 'User-Agent: ' . (isset($options['user_agent']) ? $options['user_agent'] : 'Mozilla/5.0 (compatible; ' . php_uname('s') . ')'), 'Connection: close']);
if (isset($options['content']) && is_string($options['content'])) {
if ($options['method'] == 'POST' && !preg_grep('/^Content-Type:/', $options['header'])) {
$this->subst($options['header'], 'Content-Type: application/x-www-form-urlencoded');
}
$this->subst($options['header'], 'Content-Length: ' . strlen($options['content']));
}
if (isset($parts['user'], $parts['pass'])) {
$this->subst($options['header'], 'Authorization: Basic ' . base64_encode($parts['user'] . ':' . $parts['pass']));
}
$options += ['method' => 'GET', 'header' => $options['header'], 'follow_location' => TRUE, 'max_redirects' => 20, 'ignore_errors' => FALSE];
$eol = "\r\n";
if ($fw->get('CACHE') && preg_match('/GET|HEAD/', $options['method'])) {
$cache = Cache::instance();
if ($cache->exists($hash = $fw->hash($options['method'] . ' ' . $url) . '.url', $data)) {
if (preg_match('/Last-Modified: (.+?)' . preg_quote($eol) . '/', implode($eol, $data['headers']), $mod)) {
$this->subst($options['header'], 'If-Modified-Since: ' . $mod[1]);
}
}
}
$result = $this->{'_' . $this->wrapper}($url, $options);
if ($result && isset($cache)) {
if (preg_match('/HTTP\\/1\\.\\d 304/', implode($eol, $result['headers']))) {
$result = $cache->get($hash);
$result['cached'] = TRUE;
} elseif (preg_match('/Cache-Control:(?:.*)max-age=(\\d+)(?:,?.*' . preg_quote($eol) . ')/', implode($eol, $result['headers']), $exp)) {
$cache->set($hash, $result, $exp[1]);
}
}
$req = [$options['method'] . ' ' . $url];
foreach ($options['header'] as $header) {
array_push($req, $header);
}
return array_merge(['request' => $req], $result);
}