mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 22:09:48 +08:00
Merge pull request #2111 from dizews/get-headers
Added ability to get incoming headers
This commit is contained in:
@@ -36,13 +36,15 @@ class RequestPanel extends Panel
|
||||
|
||||
public function save()
|
||||
{
|
||||
if (function_exists('apache_request_headers')) {
|
||||
$requestHeaders = apache_request_headers();
|
||||
} elseif (function_exists('http_get_request_headers')) {
|
||||
$requestHeaders = http_get_request_headers();
|
||||
$headers = Yii::$app->getRequest()->getHeaders();
|
||||
foreach ($headers as $name => $value) {
|
||||
if (is_array($value) && count($value) == 1) {
|
||||
$requestHeaders[$name] = current($value);
|
||||
} else {
|
||||
$requestHeaders = [];
|
||||
$requestHeaders[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$responseHeaders = [];
|
||||
foreach (headers_list() as $header) {
|
||||
if (($pos = strpos($header, ':')) !== false) {
|
||||
|
||||
@@ -93,6 +93,7 @@ Yii Framework 2 Change Log
|
||||
- Enh #1839: Added support for getting file extension and basename from uploaded file (anfrantic)
|
||||
- Enh: yii\codeception\TestCase now supports loading and using fixtures via Yii fixture framework (qiangxue)
|
||||
- Enh: Added support to parse json request data to Request::getRestParams() (cebe)
|
||||
- Enh: Added ability to get incoming headers (dizews)
|
||||
- Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue)
|
||||
- Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue)
|
||||
- Chg #1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected (qiangxue)
|
||||
|
||||
@@ -151,6 +151,10 @@ class Request extends \yii\base\Request
|
||||
|
||||
private $_cookies;
|
||||
|
||||
/**
|
||||
* @var array the headers in this collection (indexed by the header names)
|
||||
*/
|
||||
private $_headers;
|
||||
|
||||
/**
|
||||
* Resolves the current request into a route and the associated parameters.
|
||||
@@ -169,6 +173,37 @@ class Request extends \yii\base\Request
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the header collection.
|
||||
* The header collection contains incoming HTTP headers.
|
||||
* @return HeaderCollection the header collection
|
||||
*/
|
||||
public function getHeaders()
|
||||
{
|
||||
if ($this->_headers === null) {
|
||||
$this->_headers = new HeaderCollection;
|
||||
$headers = [];
|
||||
if (function_exists('getallheaders')) {
|
||||
$headers = getallheaders();
|
||||
} elseif (function_exists('http_get_request_headers')) {
|
||||
$headers = http_get_request_headers();
|
||||
} else {
|
||||
foreach ($_SERVER as $name => $value) {
|
||||
if (substr($name, 0, 5) == 'HTTP_') {
|
||||
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
|
||||
$this->_headers->add($name, $value);
|
||||
}
|
||||
}
|
||||
return $this->_headers;
|
||||
}
|
||||
foreach ($headers as $name => $value) {
|
||||
$this->_headers->add($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE).
|
||||
* @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE.
|
||||
|
||||
Reference in New Issue
Block a user