Class yii\httpclient\Response
Inheritance | yii\httpclient\Response » yii\httpclient\Message » yii\base\Component |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-httpclient/blob/master/src/Response.php |
Response represents HTTP request response.
Public 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
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 = 20x) | 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
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
Method Details
Defined in: yii\httpclient\Message::__toString()
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 '';
}
}
Defined in: yii\httpclient\Message::addCookies()
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;
}
Defined in: yii\httpclient\Message::addData()
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;
}
Defined in: yii\httpclient\Message::addHeaders()
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;
}
Defined in: yii\httpclient\Message::composeHeaderLines()
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;
}
Returns default format automatically detected from headers and content.
protected string|null defaultFormat ( ) | ||
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;
}
Detects response format from raw content.
protected null|string detectFormatByContent ( $content ) | ||
$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;
}
Detects format from headers.
protected null|string detectFormatByHeaders ( \yii\web\HeaderCollection $headers ) | ||
$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;
}
Defined in: yii\httpclient\Message::getContent()
Returns HTTP message raw content.
public string getContent ( ) | ||
return | string |
Raw body. |
---|
public function getContent()
{
return $this->_content;
}
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()
{
$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;
}
Returns the data fields, parsed from raw content.
public mixed getData ( ) | ||
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;
}
Defined in: yii\httpclient\Message::getFormat()
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;
}
Defined in: yii\httpclient\Message::getHeaders()
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;
}
Checks if response status code is OK (status code = 20x)
public boolean getIsOk ( ) | ||
return | boolean |
Whether response is OK. |
---|---|---|
throws | yii\httpclient\Exception |
public function getIsOk()
{
return strncmp('20', $this->getStatusCode(), 2) === 0;
}
Returns status code.
public string getStatusCode ( ) | ||
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.');
}
Defined in: yii\httpclient\Message::hasContent()
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]);
}
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 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);
}
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 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);
}
Defined in: yii\httpclient\Message::setContent()
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;
}
Defined in: yii\httpclient\Message::setCookies()
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;
}
Defined in: yii\httpclient\Message::setData()
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;
}
Defined in: yii\httpclient\Message::setFormat()
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;
}
Defined in: yii\httpclient\Message::setHeaders()
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;
}
Defined in: yii\httpclient\Message::toString()
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;
}