3

I keep getting this error

Warning: file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

Code

$url = BASE_URL . 'api/v1/users';
$options = array('http' => array(
    'method'  => 'GET',
    'header' => 'Authorization: Bearer '.$token
));
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);

The middleware i am using with my api accepts the authorization via url parameter or header;

If I use the token in the parameter, it works but not being able to send it via header on the server, cause on localhost its working,

the request is authorized via the bearer token i do not have any username and password to authorize the request with as i demonstrate below i only use a token in my url parameter.

any idea why ? what can i do without changing all my requests to curl

$result = file_get_contents(BASE_URL . 'api/v1/users?authorization='.$token, false);
$response = json_decode($result, true);
mirvatJ
  • 366
  • 1
  • 3
  • 15

4 Answers4

5

You can add headers to file_get_contents, it takes a parameter called context that can be used for that:

$url = BASE_URL . 'api/v1/users';
$options = array('http' => array(
    'method'  => 'GET',
    'header' => 'Authorization: Bearer '.$token
));
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);
  • i don't understand what did you change ? – mirvatJ Jul 31 '17 at 07:21
  • I have passed the header as you pass in the curl so it get the bearer and error get remove –  Jul 31 '17 at 07:22
  • 5
    i just comparer my code with yours on [diffchecker](http://diffchecker.com) and its 100% the same, plus tried it on the server and nothing changed – mirvatJ Jul 31 '17 at 07:31
4

i figured it out it was an apache and php server configuration affecting the header authorization, i was able to override it using .htaccess

i added this line in .htaccess

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
mirvatJ
  • 366
  • 1
  • 3
  • 15
0

Your problem maybe for your authorization, do you know about type of authorization in server?

it's possible your type must be cn389ncoiwuencr, say from server about that and try:

'header' => 'Authorization: Bearer cn389ncoiwuencr '.$token
Hamid Abbasi
  • 346
  • 2
  • 14
  • i am using this library [github](https://github.com/dyorg/slim-token-authentication/) for authentication – mirvatJ Jul 31 '17 at 07:26
  • here you can find some tips about 401 error on this library: https://github.com/dyorg/slim-token-authentication/#error – Hamid Abbasi Jul 31 '17 at 13:55
-2

You can use CURL for make a request

 $opts = array(
                'http'=>array(
                    'method'=>"GET",
                    'header'=>"Content-Type: application/json en\r\n" .
                    "Authorization: Token ".$token."\r\n"
                )
            );

$response = file_get_contents($Base_URL."?format=json", false, stream_context_create($opts));

echo $response;
Varun Malhotra
  • 1,202
  • 3
  • 15
  • 28
  • is there a solution to it without migrating all my code to curl ? – mirvatJ Jul 31 '17 at 07:19
  • i did, now the error doesn't show up, but the data is not retrieved, ps: i am using php 7 on both localhost and server but on localhost it works – mirvatJ Jul 31 '17 at 07:24
  • i just tried calling a endpoint that doesn't need authentication and it works when i remove header authorization bearer, doesn't that mean that file_get_content is working normally ? – mirvatJ Jul 31 '17 at 07:40
  • i don't understand your code you are suggesting is exactly like the one i posted, nothing different at all ? – mirvatJ Jul 31 '17 at 07:43
  • file_get_content is deprecated in php 5.5 and upper versions. – Varun Malhotra Jul 31 '17 at 07:56
  • if you want to use the file_get_content then you have to set your PHP version at 5.4 but i would refer you to use Curl – Varun Malhotra Jul 31 '17 at 07:57
  • i did set my php to 5.4, now the error doesn't show up, but the data is not retrieved; ps: i am using php 7 on both localhost and server but on localhost it works. and while it was set on 7 I tried calling a endpoint that doesn't need authentication and it works when i remove header authorization bearer, doesn't that mean that file_get_content is working normally ? – mirvatJ Jul 31 '17 at 08:02
  • PHP recommends not to use the deprecated functions – Varun Malhotra Jul 31 '17 at 08:08
  • I understand Varun but can you help me figure it out ? instead of changing all my code ? – mirvatJ Jul 31 '17 at 08:09
  • 3
    @VarunMalhotra Where did you get the idea that this is deprecated? There's nothing about this at http://php.net/manual/en/function.file-get-contents.php. – Barmar Jan 24 '18 at 15:54
  • It's 2021, PHP 8+ is released and `file_get_contents` is still not deprecated. – MLK.DEV Aug 18 '21 at 15:08