PicoFeed\Client\Stream::doRequest PHP Метод

doRequest() публичный Метод

Do the HTTP request.
public doRequest ( ) : array
Результат array HTTP response ['body' => ..., 'status' => ..., 'headers' => ...]
    public function doRequest()
    {
        $body = '';
        // Create context
        $context = stream_context_create($this->prepareContext());
        // Make HTTP request
        $stream = @fopen($this->url, 'r', false, $context);
        if (!is_resource($stream)) {
            throw new InvalidUrlException('Unable to establish a connection');
        }
        // Get HTTP headers response
        $metadata = stream_get_meta_data($stream);
        list($status, $headers) = HttpHeaders::parse($metadata['wrapper_data']);
        if ($this->isPassthroughEnabled()) {
            header(':', true, $status);
            if (isset($headers['Content-Type'])) {
                header('Content-Type: ' . $headers['Content-Type']);
            }
            fpassthru($stream);
        } else {
            // Get the entire body until the max size
            $body = stream_get_contents($stream, $this->max_body_size + 1);
            // If the body size is too large abort everything
            if (strlen($body) > $this->max_body_size) {
                throw new MaxSizeException('Content size too large');
            }
            if ($metadata['timed_out']) {
                throw new TimeoutException('Operation timeout');
            }
        }
        fclose($stream);
        $this->setEffectiveUrl($metadata['wrapper_data']);
        return array('status' => $status, 'body' => $this->decodeBody($body, $headers), 'headers' => $headers);
    }

Usage Example

Пример #1
0
 /**
  * @group online
  */
 public function testDecodeGzip()
 {
     if (function_exists('gzdecode')) {
         $client = new Stream();
         $client->setUrl('https://github.com/fguillot/picoFeed');
         $result = $client->doRequest();
         $this->assertEquals('gzip', $result['headers']['Content-Encoding']);
         $this->assertEquals('<!DOC', substr(trim($result['body']), 0, 5));
     }
 }