RestServer in Codeigniter: 401 Unauthorized response when loading in view

3.2k views Asked by At

I'm using the restserver in Codeigniter from Chris Kacerguis and I setup rest_auth with session. When I'm logged in, I access the page whose view.php get the response through file_get_contents and it's returning 401 Unauthorized. When I access the API's direct page in a new tab, it returns the json correctly.

the view.php is loading the API content in this way:

json_decode(file_get_contents('https://example.com/api/proces/'), true);

the response:

A PHP Error was encountered

Severity: Warning

Message: file_get_contents( I needed to remove the link ): failed to open >stream: HTTP request failed! HTTP/1.1 401 Unauthorized

What's happening?

1

There are 1 answers

1
skribe On

I see your example url is using "https" and it is possible that your Apache / Nginx server is not configured correctly to handle the SSL in this way, but it would work fine with the certificate in the browser.

I would suggest you use curl as an alternative instead.

json_decode(curl_get_contents('https://example.com/api/process/'));

from here https://github.com/zg/curl_get_contents

Or of course you could use curl directly.

$url = 'https://example.com/api/process/';
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HEADER, false);
$data = curl_exec($request);
curl_close($request);