user1530205
user1530205

Reputation: 302

Codeigniter API error returning

Hi has anyone any experience using Phil Sturgeons RESTFUL libraries for codeigniter. I've decided to create a web service for our database in order to supply access to the database from multiple applications. The website is currently developed in Codeigniter therefore it was a simple solution to use the rest API libraries.

The problem I have is that I am trying to return specific errors in the event of a problem.

At the moment I am purposely returning an error like so:

require(APPPATH . 'libraries/REST_Controller.php');

class Settings_api extends REST_Controller {

    function settings_get()
    {
        $this->response(NULL, 404);
    }
}

If I access the url directly then I am just receiving a blank page, I can return a message if I replace the 'NULL' with a message but there is nothing to say its a 404 error whereas If I call the page via php using the following

$user = json_decode(file_get_contents('http://www.example.co.uk/api/settings_api/settings/'));

echo $user;

then it shows the following line

Message: file_get_contents(http://www.example.co.uk/api/settings_api/settings/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 

In both instances I would like to return a 404 error along with a message I provide. Is this possible and if so could you point me in the right direction.

Thanks

Upvotes: 0

Views: 2888

Answers (1)

aefxx
aefxx

Reputation: 25249

The error message being generated by PHP, as far as I know, there's nothing you can do about this (other than using the @ operator, which I do NOT recommend). So, your only option is to manually check file_get_content()'s return value:

$response = file_get_contents('http://...');

if ($response === false) {
    // return whatever you feel is appropriate
} else {
    $user = json_decode($response);
    echo $user;
}

EDIT
Found this answer here on Stackoverflow which is what you are looking for.

Upvotes: 2

Related Questions