1

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.

Web Artisan
  • 1,870
  • 3
  • 23
  • 33
Jacob Nelson
  • 2,370
  • 23
  • 35
  • What does your last code block should do? It simply concatenates strings, but the result isn't used in any way, it's neither returned nor it is echoed anywhere. If you want to have a look at 'unknown variables' or arrays, try [var_dump()](http://php.net/manual/en/function.var-dump.php) and post the output. (you should post the output of `var_dump($request->getHeaders());`) – tillz Apr 29 '16 at 07:54

1 Answers1

3

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.

Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49