fake_rest_server – REST server in PHP
Tags: rest, server, php, library
Published: 2018-11-30 16:51:10

---
title: fake_rest_server – REST server in PHP
published: true
tags: rest,server,php,library
canonical_url: 
---

“[fake\_rest\_server](https://github.com/jalbam/fake_rest_server)” is a simple and easy-to-configure REST server made in PHP.

I made it just to create different REST servers very fast for testing purposes.

It can be found on GitHub: [https://github.com/jalbam/fake\_rest\_server](https://github.com/jalbam/fake_rest_server)

## Configuring the server

To add new paths (routes) and methods, the developer just needs to create a new folder structure (folder and subfolders if needed) which represents the route and inside one file per method named _[method\_desired].php_ (for example, _put.php_).

To start developing your REST server, you will only need to download the files inside the **[src/](https://github.com/jalbam/fake_rest_server/blob/master/src)** folder.

You may want to take a look at the **[src/\_code/functions.php](https://github.com/jalbam/fake_rest_server/blob/master/src/_code/functions.php)** file (the engine will include it automatically) as it provides some basic but useful functions. There you can also add new functions or modify the existing ones.

If you want to add data, you can use the **[src/\_code/data/data.php](https://github.com/jalbam/fake_rest_server/blob/master/src/_code/data/data.php)** file (automatically included by the engine).

If you want to add configuration data, you can use the **[src/\_code/config.php](https://github.com/jalbam/fake_rest_server/blob/master/src/_code/config.php)** file (automatically included by the engine).

As the engine defines the **USING\_REST\_SERVER** constant, you can protect any of the files with the following line at the beginning:

```php
<?php if (!defined("USING_REST_SERVER") || !USING_REST_SERVER) { return; } ?>
```

Note: this authorization code above is also defined in the **AUTHORIZATION\_CODE** constant (inside the the **[src/\_code/config.php](https://github.com/jalbam/fake_rest_server/blob/master/src/_code/config.php)** file).

### Example:

1) In the root folder (where the **[src/\_code/](https://github.com/jalbam/fake_rest_server/blob/master/src/_code/)** folder and the **[src/index.php](https://github.com/jalbam/fake_rest_server/blob/master/src/index.php)** file are placed), create a folder called **myRESTService/**.

2) Inside the **myRESTService/** folder we have just created, create another folder called **user/** and inside of it create two files: **index.php** and **get.php**.

3) Inside the **myRESTService/user/index.php** file, place the following code:

```php
<?php
	//Users info (this could be in the "src/_code/config.php" file, but it is just an example):
	$usersData = Array
	(
		//User IDs:
		"1" =>
			Array
			(
				"name" => "John Doe",
				"favouriteFood" => "meat"
			),
		"2" =>
			Array
			(
				"name" => "Joan Alba Maldonado",
				"favouriteFood" => "pizza"
			)
	);
	
	//Gets the data needed which has been sent through the REST client:
	$userId = getVariable("id"); //"getVariable" and other functions available in the "src/_code/functions.php" file.
```

4) Inside the **myRESTService/user/get.php** file put the following code:

```php
<?php
	if ($userId === "") { echo "No id sent!"; }
	else if (array_key_exists($userId, $usersData))
	{
		echo $usersData[$userId]["name"] . " likes eating " . $usersData[$userId]["favouriteFood"];
	}
	else { echo "User cannot be found! (id=" . $userId . ")"; }
```

5) With this, we will have our REST server configured with the **myRESTService/user/** route, accepting the **GET** method with the **id** parameter. This example can be found in the **[example\_easy/](https://github.com/jalbam/fake_rest_server/blob/master/example_easy)** folder.

## Testing the server

If you do not have a REST client, the server can be tested on any web browser by adding the **debug=1** parameter to the URL as well as the **method** parameter with the method desired (not needed if the method is **GET** ), as for example: _http://localhost/fake\_rest\_server/src/index.php/route\_1/subroute?method=post&debug=1&username=Joan_

Following the example above, you can use a web browser to visit the following links:

_http://localhost/route\_to\_the\_REST\_server/index.php/myRESTService/user/?method=get&debug=1&id=1_ (it should show “John Doe likes eating meat”)

_http://localhost/route\_to\_the\_REST\_server/index.php/myRESTService/user/?method=get&debug=1&id=2_ (it should show “Joan Alba Maldonado likes eating pizza”)

_http://localhost/route\_to\_the\_REST\_server/index.php/myRESTService/user/?method=get&debug=1&id=3_ (it should show “User cannot be found! (id=3)”)

_http://localhost/route\_to\_the\_REST\_server/index.php/myRESTService/user/?method=get&debug=1_ (it should show “No id sent!”)

Note that the “_/_” character at the end of the route is optional.

## Final comments

It is very easy to extend using PHP language. The project already includes some examples with routes, methods, functions and data as user accounts, etc. as examples (in both the **[example/](https://github.com/jalbam/fake_rest_server/blob/master/example)** and the **[example\_easy/](https://github.com/jalbam/fake_rest_server/blob/master/example_easy)** folder) but they can be deleted.

The only really-needed code is located in the **[src/](https://github.com/jalbam/fake_rest_server/blob/master/src/)** folder. Inside of it, the **[route\_1/](https://github.com/jalbam/fake_rest_server/blob/master/src/route_1)** folder and all of its content can also be deleted since it is just an example.

## License

This project can be used, reproduced, distributed and modified freely for any non-commercial purposes but always keeping the author’s name and copyright clauses. Other than that, just use this project as you wish but never sell it!