dsmnedc798226
2016-04-29 05:24
浏览 47
已采纳

slim框架PSR无法识别自定义标头

Following is the code I am using.

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

function handle_response($output, $response){
    if($output['status'] === "failed"){
        $response = $response->withStatus(400);
        $response->getBody()->write(json_encode($output));
    }
    else{
        $response->getBody()->write(json_encode($output));
    }
    return $response;

}

/* user profile */
$app->get('/user/profile', function (Request $request, Response $response) {
$headers = $request->getHeaders();
foreach ($headers as $name => $values) {
$name . ": " . implode(", ", $values);
}
    if ($request->hasHeader('Authorization')) {
    $output['token'] = 'yes';
}
    else
        $output['token'] = $name;

        $output['status'] = "success";    
        return handle_response($output, $response);
});

I am using postman in google chrome to test this REST service. I am passing a custom header "Authorization" along with the GET request. however, this program doesn't recognize any custom headers.

The code block

$headers = $request->getHeaders();
foreach ($headers as $name => $values) {
     $name . ": " . implode(", ", $values);
}

only returns the header "HTTP_ACCEPT_LANGUAGE".

Please help me to figure out, what went wrong here.

图片转代码服务由CSDN问答提供 功能建议

以下是我正在使用的代码。

 &lt;  ?php 
use \ Psr \ Http \ Message \ ServerRequestInterface as Request; 
use \ Psr \ Http \ Message \ ResponseInterface as Response; 
 
 $ app = new \ Slim \ App; 
 
function handle_response($ output,  $ response if {$ n if($ output ['status'] ===“failed”){
 $ response = $ response-&gt; withStatus(400); 
 $ response-&gt; getBody() - &gt  ;写(json_encode($ output)); 
} 
 else {
 $ response-&gt; getBody() - &gt; write(json_encode($ output)); 
} 
返回$ response; 
  
} 
 
 / *用户个人资料* / 
 $ app-&gt; get('/ user / profile',function(Request $ request,Response $ response){
 $ headers = $ request-&gt;  getHeaders(); 
foreach($ headers as $ name =&gt; $ values){
 $ name。“:”。implode(“,”,$ values); 
} 
 if($ request-&gt;  hasHeader('Authorization')){
 $ output ['token'] ='yes'; 
} 
 else 
 $ output ['token'] = $ name; 
 
 $ output ['status  '] =“成功”; 
返回ha  ndle_response($ output,$ response); 
}); 
   
 
 

我在谷歌浏览器中使用postman来测试这个REST服务。 我正在传递一个自定义 标题“授权”以及GET请求。 或者,此程序无法识别任何自定义标题。

代码块

  $ headers = $ request-&gt; getHeaders(); 
foreach($ headers as $ name =&gt;  $ values){
 $ name。  “:”。  implode(“,”,$ values); 
} 
   
 
 

只返回标题“HTTP_ACCEPT_LANGUAGE”。

请 帮我弄清楚,这里出了什么问题。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhanlie9209 2016-04-29 12:42
    已采纳

    It is not Slim, it is considered a PHP feature. If you are sending something else than valid HTTP Basic Authorization header, PHP will not have access to it. You can work around this by adding the following rewrite rule to your .htaccess file.

    RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    

    Another way is to use apache_request_headers() but it is Apache specific and not portable.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题