user3182261
user3182261

Reputation: 271

Throw correct HTTP header with PHP

With the webservice I'm making, users are able to go to a "detail" page by entering an Id in the URI.

So far I'm able to get the data from the given Id by using the following code

$id = $_GET['id'];
        $file = file_get_contents("data.json");
        $json = json_decode($file);


        foreach ($json->items as $item) {
            if ($item->id == $id) {
                $json = json_encode($item);

                $header = $_SERVER['HTTP_ACCEPT'];

                switch ($header) {
                    case "application/json":
                        header('Content-Type: application/json');
                        echo $json;
                        break;
                    case "application/xml":
                        header('Content-type: application/xml');
                        $serializer = &new XML_Serializer();
                        $obj = json_decode($json);

                        if ($serializer->serialize($obj)) {
                            echo $serializer->getSerializedData();
                        } else {
                            return null;
                        }
                        break;
                    default:
                        header('HTTP/1.1 415 Unsupported Media Type');
                        echo json_encode(["message" => "Unsupported format. Choose JSON or XML"]);
                        break;
                }
            }
        }

Now my probem is, when a user enters an Id that is not in the JSON file, I still return a 200 OK. I want to return a 200 OK when an Id(and with that some data) is found and a 404 Not Found when the entered Id is not found in the JSON.

Any idea on how I can do this?

Upvotes: 0

Views: 91

Answers (1)

Soley
Soley

Reputation: 1776

$id = $_GET['id'];
        $file = file_get_contents("data.json");
        $json = json_decode($file);

$item = null;
foreach ($json->items as $tmp) {
 if ($tmp->id == $id) {
   $item = $tmp;
   break;
 }
}

if ($item == null) {
  header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
} else {
  // your own code you used above.

header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
 $json = json_encode($item);

                $header = $_SERVER['HTTP_ACCEPT'];

                switch ($header) {
                    case "application/json":
                        header('Content-Type: application/json');
                        echo $json;
                        break;
                    case "application/xml":
                        header('Content-type: application/xml');
                        $serializer = &new XML_Serializer();
                        $obj = json_decode($json);

                        if ($serializer->serialize($obj)) {
                            echo $serializer->getSerializedData();
                        } else {
                            return null;
                        }
                        break;
                    default:
                        header('HTTP/1.1 415 Unsupported Media Type');
                        echo json_encode(["message" => "Unsupported format. Choose JSON or XML"]);
                        break;
                }

}

Upvotes: 1

Related Questions