Stubbing out your
REST services

Quick and easy PHP craftiness

I’ve been doing a lot of work with REST API services recently but needed a quick way of stubbing some of them out. Here is a simple way of doing it in PHP.

PHP
            
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo <<< EOT
{
    "count":1,
    "templateHeaders":[
        {
            "vendor":"Apple",
            "projectType":"eventTicket",
            "projectId":40254,
            "type":"Event Ticket",
            "vendorId":1,
            "deleted":"False",
            "id":"39052",
            "updatedAt":"2015-01-27T00:09:48Z",
            "description":"Zoom City Pass",
            "createdAt":"2015-01-26T21:34:24Z",
            "name":"Zoom City Pass (2)",
            "disabled":"False"
        }
    ],
    "pagination":{
        "order":"name",
        "page":1,
        "start":0,
        "direction":"DESC",
        "pageSize":100
      }
}

EOT
?>
        

The PHP adds the necessary headers to make the response available across origins and return as JSON. The body of the JSON is contained inside a multiline PHP Echo using the heredoc syntax. I published this to a sub-domain on my site using a REST-like URL http://stub.stphnsn.com/rest/v1/example/ then pointed my development code to use the fake service.