Class yii\httpclient\Response

Inheritanceyii\httpclient\Response » yii\httpclient\Message » yii\base\Component
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-httpclient/blob/master/src/Response.php

Response represents HTTP request response.

Public Properties

Hide inherited properties

Property Type Description Defined By
$client yii\httpclient\Client Owner client instance. yii\httpclient\Message
$content string Raw body. yii\httpclient\Message
$cookies \yii\web\CookieCollection|\yii\web\Cookie[] The cookie collection. yii\httpclient\Message
$data mixed Content data fields. yii\httpclient\Message
$format string Body format name. yii\httpclient\Message
$headers \yii\web\HeaderCollection The header collection. yii\httpclient\Message
$isOk boolean Whether response is OK. yii\httpclient\Response
$statusCode string Status code. yii\httpclient\Response

Public Methods

Hide inherited methods

Method Description Defined By
__toString() PHP magic method that returns the string representation of this object. yii\httpclient\Message
addCookies() Adds more cookies to the already defined ones. yii\httpclient\Message
addData() Adds data fields to the existing ones. yii\httpclient\Message
addHeaders() Adds more headers to the already defined ones. yii\httpclient\Message
composeHeaderLines() Composes raw header lines from $headers. yii\httpclient\Message
getContent() Returns HTTP message raw content. yii\httpclient\Message
getCookies() Returns the cookie collection. yii\httpclient\Response
getData() Returns the data fields, parsed from raw content. yii\httpclient\Response
getFormat() Returns body format. yii\httpclient\Message
getHeaders() Returns the header collection. yii\httpclient\Message
getIsOk() Checks if response status code is OK (status code = 2xx) yii\httpclient\Response
getStatusCode() Returns status code. yii\httpclient\Response
hasContent() Checks if content with provided name exists yii\httpclient\Message
hasCookies() Checks of HTTP message contains any cookie. yii\httpclient\Message
hasHeaders() Checks of HTTP message contains any header. yii\httpclient\Message
setContent() Sets the HTTP message raw content. yii\httpclient\Message
setCookies() Sets the cookies associated with HTTP message. yii\httpclient\Message
setData() Sets the data fields, which composes message content. yii\httpclient\Message
setFormat() Sets body format. yii\httpclient\Message
setHeaders() Sets the HTTP headers associated with HTTP message. yii\httpclient\Message
toString() Returns string representation of this HTTP message. yii\httpclient\Message

Protected Methods

Hide inherited methods

Method Description Defined By
defaultFormat() Returns default format automatically detected from headers and content. yii\httpclient\Response
detectFormatByContent() Detects response format from raw content. yii\httpclient\Response
detectFormatByHeaders() Detects format from headers. yii\httpclient\Response

Property Details

Hide inherited properties

$isOk public property

Whether response is OK.

public boolean $isOk null
$statusCode public property

Status code.

public string $statusCode null

Method Details

Hide inherited methods

__toString() public method

Defined in: yii\httpclient\Message::__toString()

PHP magic method that returns the string representation of this object.

public __toString( ): string
return string

The string representation of this object.

                public function __toString()
{
    // __toString cannot throw exception
    // use trigger_error to bypass this limitation
    try {
        return $this->toString();
    } catch (\Exception $e) {
        ErrorHandler::convertExceptionToError($e);
        return '';
    }
}

            
addCookies() public method

Defined in: yii\httpclient\Message::addCookies()

Adds more cookies to the already defined ones.

public addCookies( \yii\web\Cookie[]|array $cookies ): $this
$cookies \yii\web\Cookie[]|array

Additional cookies.

return $this

Self reference.

                public function addCookies(array $cookies)
{
    $cookieCollection = $this->getCookies();
    foreach ($cookies as $cookie) {
        if (!is_object($cookie)) {
            $cookie = new Cookie($cookie);
        }
        $cookieCollection->add($cookie);
    }
    return $this;
}

            
addData() public method (available since version 2.0.1)

Defined in: yii\httpclient\Message::addData()

Adds data fields to the existing ones.

public addData( array $data ): $this
$data array

Additional content data fields.

return $this

Self reference.

                public function addData($data)
{
    if (empty($this->_data)) {
        $this->_data = $data;
    } else {
        if (!is_array($this->_data)) {
            throw new \yii\base\Exception('Unable to merge existing data with new data. Existing data is not an array.');
        }
        $this->_data = array_merge($this->_data, $data);
    }
    return $this;
}

            
addHeaders() public method

Defined in: yii\httpclient\Message::addHeaders()

Adds more headers to the already defined ones.

public addHeaders( array $headers ): $this
$headers array

Additional headers in format: [headerName => headerValue]

return $this

Self reference.

                public function addHeaders(array $headers)
{
    $headerCollection = $this->getHeaders();
    foreach ($headers as $name => $value) {
        $headerCollection->add($name, $value);
    }
    return $this;
}

            
composeHeaderLines() public method

Defined in: yii\httpclient\Message::composeHeaderLines()

Composes raw header lines from $headers.

Each line will be a string in format: 'header-name: value'.

public composeHeaderLines( ): array
return array

Raw header lines.

                public function composeHeaderLines()
{
    if (!$this->hasHeaders()) {
        return [];
    }
    $headers = [];
    foreach ($this->getHeaders() as $name => $values) {
        $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
        foreach ($values as $value) {
            $headers[] = "$name: $value";
        }
    }
    return $headers;
}

            
defaultFormat() protected method

Returns default format automatically detected from headers and content.

protected defaultFormat( ): string|null
return string|null

Format name, 'null' - if detection failed.

                protected function defaultFormat()
{
    $format = $this->detectFormatByHeaders($this->getHeaders());
    if ($format === null) {
        $format = $this->detectFormatByContent($this->getContent());
    }
    return $format;
}

            
detectFormatByContent() protected method

Detects response format from raw content.

protected detectFormatByContent( string $content ): null|string
$content string

Raw response content.

return null|string

Format name, 'null' - if detection failed.

                protected function detectFormatByContent($content)
{
    if (preg_match('/^(\\{|\\[\\{).*(\\}|\\}\\])$/is', $content)) {
        return Client::FORMAT_JSON;
    }
    if (preg_match('/^([^=&])+=[^=&]+(&[^=&]+=[^=&]+)*$/', $content)) {
        return Client::FORMAT_URLENCODED;
    }
    if (preg_match('/^<\?xml.*>$/s', $content)) {
        return Client::FORMAT_XML;
    }
    return null;
}

            
detectFormatByHeaders() protected method

Detects format from headers.

protected detectFormatByHeaders( \yii\web\HeaderCollection $headers ): null|string
$headers \yii\web\HeaderCollection

Source headers.

return null|string

Format name, 'null' - if detection failed.

                protected function detectFormatByHeaders(HeaderCollection $headers)
{
    $contentTypeHeaders = $headers->get('content-type', null, false);
    if (!empty($contentTypeHeaders)) {
        $contentType = end($contentTypeHeaders);
        if (stripos($contentType, 'json') !== false) {
            return Client::FORMAT_JSON;
        }
        if (stripos($contentType, 'urlencoded') !== false) {
            return Client::FORMAT_URLENCODED;
        }
        if (stripos($contentType, 'xml') !== false) {
            return Client::FORMAT_XML;
        }
    }
    return null;
}

            
getContent() public method

Defined in: yii\httpclient\Message::getContent()

Returns HTTP message raw content.

public getContent( ): string
return string

Raw body.

                public function getContent()
{
    return $this->_content;
}

            
getCookies() public method

Returns the cookie collection.

The cookie collection contains the cookies associated with HTTP message.

public getCookies( ): \yii\web\CookieCollection|\yii\web\Cookie[]
return \yii\web\CookieCollection|\yii\web\Cookie[]

The cookie collection.

                public function getCookies()
{
    $cookieCollection = parent::getCookies();
    if ($cookieCollection->getCount() === 0 && $this->getHeaders()->has('set-cookie')) {
        $cookieStrings = $this->getHeaders()->get('set-cookie', [], false);
        foreach ($cookieStrings as $cookieString) {
            $cookieCollection->add($this->parseCookie($cookieString));
        }
    }
    return $cookieCollection;
}

            
getData() public method

Returns the data fields, parsed from raw content.

public getData( ): mixed
return mixed

Content data fields.

                public function getData()
{
    $data = parent::getData();
    if ($data === null) {
        $content = $this->getContent();
        if (is_string($content) && strlen($content) > 0) {
            $data = $this->getParser()->parse($this);
            $this->setData($data);
        }
    }
    return $data;
}

            
getFormat() public method

Defined in: yii\httpclient\Message::getFormat()

Returns body format.

public getFormat( ): string
return string

Body format name.

                public function getFormat()
{
    if ($this->_format === null) {
        $this->_format = $this->defaultFormat();
    }
    return $this->_format;
}

            
getHeaders() public method

Defined in: yii\httpclient\Message::getHeaders()

Returns the header collection.

The header collection contains the HTTP headers associated with HTTP message.

public getHeaders( ): \yii\web\HeaderCollection
return \yii\web\HeaderCollection

The header collection

                public function getHeaders()
{
    if (!is_object($this->_headers)) {
        $headerCollection = new HeaderCollection();
        if (is_array($this->_headers)) {
            foreach ($this->_headers as $name => $value) {
                if (is_int($name)) {
                    // parse raw header :
                    $rawHeader = $value;
                    if (strpos($rawHeader, 'HTTP/') === 0) {
                        $parts = explode(' ', $rawHeader, 3);
                        $headerCollection->add('http-code', $parts[1]);
                    } elseif (($separatorPos = strpos($rawHeader, ':')) !== false) {
                        $name = strtolower(trim(substr($rawHeader, 0, $separatorPos)));
                        $value = trim(substr($rawHeader, $separatorPos + 1));
                        $headerCollection->add($name, $value);
                    } else {
                        $headerCollection->add('raw', $rawHeader);
                    }
                } else {
                    $headerCollection->set($name, $value);
                }
            }
        }
        $this->_headers = $headerCollection;
    }
    return $this->_headers;
}

            
getIsOk() public method

Checks if response status code is OK (status code = 2xx)

public getIsOk( ): boolean
return boolean

Whether response is OK.

throws yii\httpclient\Exception

                public function getIsOk()
{
    $statusCode = (int)$this->getStatusCode();
    return $statusCode >= 200 && $statusCode < 300;
}

            
getStatusCode() public method

Returns status code.

public getStatusCode( ): string
return string

Status code.

throws yii\httpclient\Exception

on failure.

                public function getStatusCode()
{
    $headers = $this->getHeaders();
    if ($headers->has('http-code')) {
        // take into account possible 'follow location'
        $statusCodeHeaders = $headers->get('http-code', null, false);
        return empty($statusCodeHeaders) ? null : end($statusCodeHeaders);
    }
    throw new Exception('Unable to get status code: referred header information is missing.');
}

            
hasContent() public method (available since version 2.0.10)

Defined in: yii\httpclient\Message::hasContent()

Checks if content with provided name exists

public hasContent( mixed $key ): boolean
$key mixed

String Name of the content parameter

                public function hasContent($key)
{
    $content = $this->getContent();
    return is_array($content) && isset($content[$key]);
}

            
hasCookies() public method

Defined in: yii\httpclient\Message::hasCookies()

Checks of HTTP message contains any cookie.

Using this method you are able to check cookie presence without instantiating CookieCollection.

public hasCookies( ): boolean
return boolean

Whether message contains any cookie.

                public function hasCookies()
{
    if (is_object($this->_cookies)) {
        return $this->_cookies->getCount() > 0;
    }
    return !empty($this->_cookies);
}

            
hasHeaders() public method

Defined in: yii\httpclient\Message::hasHeaders()

Checks of HTTP message contains any header.

Using this method you are able to check cookie presence without instantiating HeaderCollection.

public hasHeaders( ): boolean
return boolean

Whether message contains any header.

                public function hasHeaders()
{
    if (is_object($this->_headers)) {
        return $this->_headers->getCount() > 0;
    }
    return !empty($this->_headers);
}

            
setContent() public method

Defined in: yii\httpclient\Message::setContent()

Sets the HTTP message raw content.

public setContent( string $content ): $this
$content string

Raw content.

return $this

Self reference.

                public function setContent($content)
{
    $this->_content = $content;
    return $this;
}

            
setCookies() public method

Defined in: yii\httpclient\Message::setCookies()

Sets the cookies associated with HTTP message.

public setCookies( \yii\web\CookieCollection|\yii\web\Cookie[]|array $cookies ): $this
$cookies \yii\web\CookieCollection|\yii\web\Cookie[]|array

Cookie collection or cookies list.

return $this

Self reference.

                public function setCookies($cookies)
{
    $this->_cookies = $cookies;
    return $this;
}

            
setData() public method

Defined in: yii\httpclient\Message::setData()

Sets the data fields, which composes message content.

public setData( mixed $data ): $this
$data mixed

Content data fields.

return $this

Self reference.

                public function setData($data)
{
    $this->_data = $data;
    return $this;
}

            
setFormat() public method

Defined in: yii\httpclient\Message::setFormat()

Sets body format.

public setFormat( string $format ): $this
$format string

Body format name.

return $this

Self reference.

                public function setFormat($format)
{
    $this->_format = $format;
    return $this;
}

            
setHeaders() public method

Defined in: yii\httpclient\Message::setHeaders()

Sets the HTTP headers associated with HTTP message.

public setHeaders( array|\yii\web\HeaderCollection $headers ): $this
$headers array|\yii\web\HeaderCollection

Headers collection or headers list in format: [headerName => headerValue]

return $this

Self reference.

                public function setHeaders($headers)
{
    $this->_headers = $headers;
    return $this;
}

            
toString() public method

Defined in: yii\httpclient\Message::toString()

Returns string representation of this HTTP message.

public toString( ): string
return string

The string representation of this HTTP message.

                public function toString()
{
    $result = '';
    if ($this->hasHeaders()) {
        $headers = $this->composeHeaderLines();
        $result .= implode("\n", $headers);
    }
    $content = $this->getContent();
    if ($content !== null) {
        $result .= "\n\n" . $content;
    }
    return $result;
}