Web::request PHP 메소드

request() 공개 메소드

Submit HTTP request; Use HTTP context options (described in http://www.php.net/manual/en/context.http.php) if specified; Cache the page as instructed by remote server
public request ( $url, array $options = NULL ) : array | FALSE
$url string
$options array array
리턴 array | FALSE
    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);
    }

Usage Example

예제 #1
0
파일: edit.php 프로젝트: itillawarra/cmfive
function edit_POST(Web $w)
{
    $p = $w->pathMatch("id");
    $processor_id = $p["id"];
    // Break the selected processor up into module and class
    $processor_class = $w->request("processor_class");
    $processor_expl = explode(".", $processor_class);
    // Make sure we only have two values
    if (count($processor_expl) !== 2) {
        $w->error("Missing Processor values", "/channels/listprocessors");
        exit;
    }
    // make sure the selected class exists in config
    if (!in_array($processor_expl[1], $w->moduleConf($processor_expl[0], "processors"))) {
        $w->error("Could not find processor in config", "/channels/listprocessors");
        exit;
    }
    $processor_object = $processor_id ? $w->Channel->getProcessor($processor_id) : new ChannelProcessor($w);
    $processor_object->fill($_POST);
    $processor_object->channel_id = $w->request("channel_id");
    $processor_object->module = $processor_expl[0];
    $processor_object->class = $processor_expl[1];
    $processor_object->insertOrUpdate();
    $w->msg("Processor " . ($processor_id ? "updated" : "created"), "/channels/listprocessors");
}
All Usage Examples Of Web::request