在 Yii 2.0 中报错:yii\base\ErrorException: Undefined index: HTTP_ACCEPT
1、在 Yii 2.0 中报错:yii\base\ErrorException: Undefined index: HTTP_ACCEPT,如图1
2、查看运行时日志文件:/api/runtime/logs/app.log
2020-01-20 15:09:08 [10.42.129.69][1][-][error][yii\base\ErrorException:8] yii\base\ErrorException: Undefined index: HTTP_ACCEPT in /mcloud/www/pcs-api/common/behaviors/RequestLogBehavior.php:45 Stack trace: #0 /mcloud/www/pcs-api/common/behaviors/RequestLogBehavior.php(45): yii\base\ErrorHandler->handleError(8, 'Undefined index...', '/mcloud/www/pcs...',45, Array) #1 [internal function]: common\behaviors\RequestLogBehavior->afterRequest(Object(yii\base\Event)) #2 /mcloud/www/pcs-api/vendor/yiisoft/yii2/base/Component.php(627): call_user_func(Array, Object(yii\base\Event)) #3 /mcloud/www/pcs-api/vendor/yiisoft/yii2/base/Application.php(389): yii\base\Component->trigger('afterRequest') #4 /mcloud/www/pcs-api/api/web/index.php(17): yii\base\Application->run() #5 {main} 2020-01-20 15:09:08 [10.42.129.69][1][-][warning][yii\debug\Module::checkAccess] Access to debugger is denied due to IP address restriction. The requesting IP address is 10.42.129.69 2020-01-20 15:09:07 [10.42.129.69][1][-][info][application] $_GET = [ 'login_id' => 'b871d55257295f1b779ef33fe803e37d' 'login_tid' => '3eec10b739b09b64aac91fde04297af1' ] $_POST = [ 'scenario' => 'default' ] $_FILES = [ 'files' => [ 'name' => [ 0 => 'VIDEO_20190718_151652.mp4' ] 'type' => [ 0 => '*/*' ] 'tmp_name' => [ 0 => '/tmp/phpxieQyP' ] 'error' => [ 0 => 0 ] 'size' => [ 0 => 1529188 ] ] ] $_COOKIE = [] $_SERVER = [ 'USER' => 'nginx' 'HOME' => '/var/cache/nginx' 'HTTP_USER_AGENT' => 'okhttp/3.12.0' 'HTTP_ACCEPT_ENCODING' => 'gzip' 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=893c73f9-c868-4372-80e5-13761fda8511' 'HTTP_X_REAL_IP' => '223.85.203.162' 'HTTP_X_FORWARDED_PORT' => '8000' 'HTTP_X_FORWARDED_HOST' => 'pcs.wjdev.chinamcloud.cn' 'HTTP_X_FORWARDED_PROTO' => 'http' 'HTTP_X_FORWARDED_FOR' => '140.143.139.242' 'HTTP_CONTENT_LENGTH' => '1529523' 'HTTP_HOST' => 'api.pcs.wjdev.chinamcloud.cn' 'REDIRECT_STATUS' => '200' 'SERVER_NAME' => 'api.pcs.wjdev.chinamcloud.cn' 'SERVER_PORT' => '80' 'SERVER_ADDR' => '10.42.137.79' 'REMOTE_PORT' => '51094' 'REMOTE_ADDR' => '10.42.129.69' 'SERVER_SOFTWARE' => 'nginx/cnginx' 'GATEWAY_INTERFACE' => 'CGI/1.1' 'REQUEST_SCHEME' => 'http' 'SERVER_PROTOCOL' => 'HTTP/1.0' 'DOCUMENT_ROOT' => '/mcloud/www/pcs-api/api/web' 'DOCUMENT_URI' => '/index.php' 'REQUEST_URI' => '/v1/mobile/assets/upload?login_id=b871d55257295f1b779ef33fe803e37d&login_tid=3eec10b739b09b64aac91fde04297af1' 'SCRIPT_NAME' => '/index.php' 'CONTENT_LENGTH' => '1529523' 'CONTENT_TYPE' => 'multipart/form-data; boundary=893c73f9-c868-4372-80e5-13761fda8511' 'REQUEST_METHOD' => 'POST' 'QUERY_STRING' => 'login_id=b871d55257295f1b779ef33fe803e37d&login_tid=3eec10b739b09b64aac91fde04297af1' 'SCRIPT_FILENAME' => '/mcloud/www/pcs-api/api/web/index.php' 'FCGI_ROLE' => 'RESPONDER' 'PHP_SELF' => '/index.php' 'REQUEST_TIME_FLOAT' => 1579504147.3086 'REQUEST_TIME' => 1579504147 ] 2020-01-20 15:12:42 [10.42.129.69][1][-][warning][yii\debug\Module::checkAccess] Access to debugger is denied due to IP address restriction. The requesting IP address is 10.42.129.69 2020-01-20 15:12:42 [10.42.129.69][1][-][info][application] $_GET = [ 'login_id' => 'b871d55257295f1b779ef33fe803e37d' 'login_tid' => 'e3ff923155266bb1372e1e525c9e64f8' ] $_POST = [ 'scenario' => 'default' ] $_FILES = [ 'files' => [ 'name' => [ 0 => 'IMG_20160924_140140.jpg' ] 'type' => [ 0 => 'image/jpeg' ] 'tmp_name' => [ 0 => '/tmp/phpZUGigV' ] 'error' => [ 0 => 0 ] 'size' => [ 0 => 2891230 ] ] ] $_COOKIE = [] $_SERVER = [ 'USER' => 'nginx' 'HOME' => '/var/cache/nginx' 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, br' 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=--------------------------688749261587533988420647' 'HTTP_POSTMAN_TOKEN' => '553eb424-916f-4ab8-9585-402e260bee25' 'HTTP_CACHE_CONTROL' => 'no-cache' 'HTTP_ACCEPT' => '*/*' 'HTTP_USER_AGENT' => 'PostmanRuntime/7.22.0' 'HTTP_X_REAL_IP' => '223.85.203.162' 'HTTP_X_FORWARDED_PORT' => '8000' 'HTTP_X_FORWARDED_HOST' => 'pcs.wjdev.chinamcloud.cn' 'HTTP_X_FORWARDED_PROTO' => 'http' 'HTTP_X_FORWARDED_FOR' => '140.143.139.242' 'HTTP_CONTENT_LENGTH' => '2891568' 'HTTP_HOST' => 'api.pcs.wjdev.chinamcloud.cn' 'REDIRECT_STATUS' => '200' 'SERVER_NAME' => 'api.pcs.wjdev.chinamcloud.cn' 'SERVER_PORT' => '80' 'SERVER_ADDR' => '10.42.137.79' 'REMOTE_PORT' => '52572' 'REMOTE_ADDR' => '10.42.129.69' 'SERVER_SOFTWARE' => 'nginx/cnginx' 'GATEWAY_INTERFACE' => 'CGI/1.1' 'REQUEST_SCHEME' => 'http' 'SERVER_PROTOCOL' => 'HTTP/1.0' 'DOCUMENT_ROOT' => '/mcloud/www/pcs-api/api/web' 'DOCUMENT_URI' => '/index.php' 'REQUEST_URI' => '/v1/mobile/assets/upload?login_id=b871d55257295f1b779ef33fe803e37d&login_tid=e3ff923155266bb1372e1e525c9e64f8' 'SCRIPT_NAME' => '/index.php' 'CONTENT_LENGTH' => '2891568' 'CONTENT_TYPE' => 'multipart/form-data; boundary=--------------------------688749261587533988420647' 'REQUEST_METHOD' => 'POST' 'QUERY_STRING' => 'login_id=b871d55257295f1b779ef33fe803e37d&login_tid=e3ff923155266bb1372e1e525c9e64f8' 'SCRIPT_FILENAME' => '/mcloud/www/pcs-api/api/web/index.php' 'FCGI_ROLE' => 'RESPONDER' 'PHP_SELF' => '/index.php' 'REQUEST_TIME_FLOAT' => 1579504362.4863 'REQUEST_TIME' => 1579504362 ]
3、两次请求的日志信息,发现当 $_SERVER[‘HTTP_USER_AGENT’] 的值为 okhttp/3.12.0 时,$_SERVER[‘HTTP_ACCEPT’] 不存在
'HTTP_ACCEPT' => '*/*' 'HTTP_USER_AGENT' => 'PostmanRuntime/7.22.0' 'HTTP_USER_AGENT' => 'okhttp/3.12.0'
4、要求 Android 客户端在 Headers 中明确声明 Accept,当 $_SERVER[‘HTTP_USER_AGENT’] 的值为 okhttp/3.12.0 时,$_SERVER[‘HTTP_ACCEPT’] 的值为 application/json; version=10.4。不再报错。
2020-01-20 15:39:31 [10.42.129.69][1][-][warning][yii\debug\Module::checkAccess] Access to debugger is denied due to IP address restriction. The requesting IP address is 10.42.129.69 2020-01-20 15:39:31 [10.42.129.69][1][-][info][application] $_GET = [ 'login_id' => 'b871d55257295f1b779ef33fe803e37d' 'login_tid' => '3eec10b739b09b64aac91fde04297af1' ] $_POST = [] $_FILES = [ 'files' => [ 'name' => [ 0 => 'PictureSelector_20180906_122206.mp4' ] 'type' => [ 0 => '*/*' ] 'tmp_name' => [ 0 => '/tmp/phpw8jS3T' ] 'error' => [ 0 => 0 ] 'size' => [ 0 => 11732395 ] ] ] $_COOKIE = [] $_SERVER = [ 'USER' => 'nginx' 'HOME' => '/var/cache/nginx' 'HTTP_USER_AGENT' => 'okhttp/3.12.0' 'HTTP_ACCEPT_ENCODING' => 'gzip' 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=b723009a-9c5d-4f65-a476-09e38a4caa9c' 'HTTP_ACCEPT' => 'application/json; version=10.4' 'HTTP_X_REAL_IP' => '223.85.203.162' 'HTTP_X_FORWARDED_PORT' => '8000' 'HTTP_X_FORWARDED_HOST' => 'pcs.wjdev.chinamcloud.cn' 'HTTP_X_FORWARDED_PROTO' => 'http' 'HTTP_X_FORWARDED_FOR' => '140.143.139.242' 'HTTP_CONTENT_LENGTH' => '11732622' 'HTTP_HOST' => 'api.pcs.wjdev.chinamcloud.cn' 'REDIRECT_STATUS' => '200' 'SERVER_NAME' => 'api.pcs.wjdev.chinamcloud.cn' 'SERVER_PORT' => '80' 'SERVER_ADDR' => '10.42.137.79' 'REMOTE_PORT' => '33416' 'REMOTE_ADDR' => '10.42.129.69' 'SERVER_SOFTWARE' => 'nginx/cnginx' 'GATEWAY_INTERFACE' => 'CGI/1.1' 'REQUEST_SCHEME' => 'http' 'SERVER_PROTOCOL' => 'HTTP/1.0' 'DOCUMENT_ROOT' => '/mcloud/www/pcs-api/api/web' 'DOCUMENT_URI' => '/index.php' 'REQUEST_URI' => '/v1/mobile/assets/upload?login_id=b871d55257295f1b779ef33fe803e37d&login_tid=3eec10b739b09b64aac91fde04297af1' 'SCRIPT_NAME' => '/index.php' 'CONTENT_LENGTH' => '11732622' 'CONTENT_TYPE' => 'multipart/form-data; boundary=b723009a-9c5d-4f65-a476-09e38a4caa9c' 'REQUEST_METHOD' => 'POST' 'QUERY_STRING' => 'login_id=b871d55257295f1b779ef33fe803e37d&login_tid=3eec10b739b09b64aac91fde04297af1' 'SCRIPT_FILENAME' => '/mcloud/www/pcs-api/api/web/index.php' 'FCGI_ROLE' => 'RESPONDER' 'PHP_SELF' => '/index.php' 'REQUEST_TIME_FLOAT' => 1579505971.373 'REQUEST_TIME' => 1579505971 ]
5、准备在服务端,做一下判断,当不存在 $_SERVER[‘HTTP_ACCEPT’] 时,就不使用 $_SERVER[‘HTTP_ACCEPT’]。编辑 /common/behaviors/RequestLogBehavior.php
$message = [ 'url' => $url, 'request_query_params' => $requestQueryParams, 'request_body_params' => $requestBodyParams, 'user_id' => $userId, '$_SERVER' => [ 'HTTP_ACCEPT_LANGUAGE' => $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', 'HTTP_ACCEPT' => $_SERVER['HTTP_ACCEPT'] ?? '', 'HTTP_HOST' => $_SERVER['HTTP_HOST'], 'REMOTE_ADDR' => $_SERVER['REMOTE_ADDR'] ?? '', 'REQUEST_URI' => $_SERVER['REQUEST_URI'], 'REQUEST_METHOD' => $_SERVER['REQUEST_METHOD'], 'CONTENT_TYPE' => $_SERVER['CONTENT_TYPE'] ?? '', ], ];
近期评论