Class yii\httpclient\Message

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

Message represents a base HTTP message.

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

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\Message
getData() Returns the data fields, parsed from raw content. yii\httpclient\Message
getFormat() Returns body format. yii\httpclient\Message
getHeaders() Returns the header collection. yii\httpclient\Message
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 name. yii\httpclient\Message

Property Details

Hide inherited properties

$client public property

Owner client instance.

$content public property

Raw body.

public string $content null
$cookies public property

The cookie collection. Note that the type of this property differs in getter and setter. See getCookies() and setCookies() for details.

public \yii\web\CookieCollection|\yii\web\Cookie[] $cookies null
$data public property

Content data fields.

public mixed $data null
$format public property

Body format name.

public string $format null
$headers public property

The header collection. Note that the type of this property differs in getter and setter. See getHeaders() and setHeaders() for details.

public \yii\web\HeaderCollection $headers null

Method Details

Hide inherited methods

__toString() public method

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

public string __toString ( )
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

Adds more cookies to the already defined ones.

public $this addCookies ( array $cookies )
$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)

Adds data fields to the existing ones.

public $this addData ( $data )
$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

Adds more headers to the already defined ones.

public $this addHeaders ( array $headers )
$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

Composes raw header lines from $headers.

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

public array composeHeaderLines ( )
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 name.

protected string defaultFormat ( )
return string

Default format name.

                protected function defaultFormat()
{
    return Client::FORMAT_URLENCODED;
}

            
getContent() public method

Returns HTTP message raw content.

public string getContent ( )
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 \yii\web\CookieCollection|\yii\web\Cookie[] getCookies ( )
return \yii\web\CookieCollection|\yii\web\Cookie[]

The cookie collection.

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

            
getData() public method

Returns the data fields, parsed from raw content.

public mixed getData ( )
return mixed

Content data fields.

                public function getData()
{
    return $this->_data;
}

            
getFormat() public method

Returns body format.

public string getFormat ( )
return string

Body format name.

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

            
getHeaders() public method

Returns the header collection.

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

public \yii\web\HeaderCollection getHeaders ( )
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;
}

            
hasContent() public method (available since version 2.0.10)

Checks if content with provided name exists

public boolean hasContent ( $key )
$key

String Name of the content parameter

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

            
hasCookies() public method

Checks of HTTP message contains any cookie.

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

public boolean hasCookies ( )
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

Checks of HTTP message contains any header.

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

public boolean hasHeaders ( )
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

Sets the HTTP message raw content.

public $this setContent ( $content )
$content string

Raw content.

return $this

Self reference.

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

            
setCookies() public method

Sets the cookies associated with HTTP message.

public $this setCookies ( $cookies )
$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

Sets the data fields, which composes message content.

public $this setData ( $data )
$data mixed

Content data fields.

return $this

Self reference.

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

            
setFormat() public method

Sets body format.

public $this setFormat ( $format )
$format string

Body format name.

return $this

Self reference.

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

            
setHeaders() public method

Sets the HTTP headers associated with HTTP message.

public $this setHeaders ( $headers )
$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

Returns string representation of this HTTP message.

public string toString ( )
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;
}