{"id":157,"date":"2018-11-30T15:53:48","date_gmt":"2018-11-30T15:53:48","guid":{"rendered":"http:\/\/joanalbamaldonado.com\/blog\/?p=157"},"modified":"2018-12-09T14:18:23","modified_gmt":"2018-12-09T14:18:23","slug":"fake_rest_server-rest-server-in-php","status":"publish","type":"post","link":"https:\/\/joanalbamaldonado.com\/blog\/2018\/11\/30\/fake_rest_server-rest-server-in-php\/","title":{"rendered":"fake_rest_server &#8211; REST server in PHP"},"content":{"rendered":"<p>&#8220;<a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\" target=\"_blank\" rel=\"noopener\">fake_rest_server<\/a>&#8221; is a simple and easy-to-configure REST server made in PHP.<\/p>\n<p>I made it just to create different REST servers very fast for testing purposes. It was made on 12th May 2016 (approximately).<\/p>\n<p>It can be found on GitHub: <a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/jalbam\/fake_rest_server<\/a><\/p>\n<h2>Configuring the server<\/h2>\n<p>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 <em>[method_desired].php<\/em> (for example, <em>put.php<\/em>).<\/p>\n<p>To start developing your REST server, you will only need to download the files inside the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\" target=\"_blank\" rel=\"noopener\">src\/<\/a><\/strong> folder.<\/p>\n<p>You may want to take a look at the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/_code\/functions.php\" target=\"_blank\" rel=\"noopener\">src\/_code\/functions.php<\/a><\/strong> 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.<\/p>\n<p>If you want to add data, you can use the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/_code\/data\/data.php\" target=\"_blank\" rel=\"noopener\">src\/_code\/data\/data.php<\/a><\/strong> file (automatically included by the engine).<\/p>\n<p>If you want to add configuration data, you can use the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/_code\/config.php\" target=\"_blank\" rel=\"noopener\">src\/_code\/config.php<\/a><\/strong> file (automatically included by the engine).<\/p>\n<p>As the engine defines the <strong>USING_REST_SERVER<\/strong> constant, you can protect any of the files with the following line at the beginning:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php if (!defined(\"USING_REST_SERVER\") || !USING_REST_SERVER) { return; } ?&gt;\n<\/pre>\n<p>Note: this authorization code above is also defined in the <strong>AUTHORIZATION_CODE<\/strong> constant (inside the the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/_code\/config.php\" target=\"_blank\" rel=\"noopener\">src\/_code\/config.php<\/a><\/strong> file).<\/p>\n<p><a name=\"example\"><\/a><\/p>\n<h3>Example:<\/h3>\n<p>1) In the root folder (where the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/_code\/\" target=\"_blank\" rel=\"noopener\">src\/_code\/<\/a><\/strong> folder and the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/index.php\" target=\"_blank\" rel=\"noopener\">src\/index.php<\/a><\/strong> file are placed), create a folder called <strong>myRESTService\/<\/strong>.<\/p>\n<p>2) Inside the <strong>myRESTService\/<\/strong> folder we have just created, create another folder called <strong>user\/<\/strong> and inside of it create two files: <strong>index.php<\/strong> and <strong>get.php<\/strong>.<\/p>\n<p>3) Inside the <strong>myRESTService\/user\/index.php<\/strong> file, place the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\n  \/\/Users info (this could be in the \"src\/_code\/config.php\" file, but it is just an example):\n  $usersData = Array\n  (\n    \/\/User IDs:\n    \"1\" =&gt;\n      Array\n      (\n        \"name\" =&gt; \"John Doe\",\n        \"favouriteFood\" =&gt; \"meat\"\n      ),\n    \"2\" =&gt;\n      Array\n      (\n        \"name\" =&gt; \"Joan Alba Maldonado\",\n        \"favouriteFood\" =&gt; \"pizza\"\n      )\n  );\n  \n  \/\/Gets the data needed which has been sent through the REST client:\n  $userId = getVariable(\"id\"); \/\/\"getVariable\" and other functions available in the \"src\/_code\/functions.php\" file.\n<\/pre>\n<p>4) Inside the <strong>myRESTService\/user\/get.php<\/strong> file put the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\n  if ($userId === \"\") { echo \"No id sent!\"; }\n  else if (array_key_exists($userId, $usersData))\n  {\n    echo $usersData[$userId][\"name\"] . \" likes eating \" . $usersData[$userId][\"favouriteFood\"];\n  }\n  else { echo \"User cannot be found! (id=\" . $userId . \")\"; }<\/pre>\n<p>5) With this, we will have our REST server configured with the <strong>myRESTService\/user\/<\/strong> route, accepting the <strong>GET<\/strong> method with the <strong>id<\/strong> parameter. This example can be found in the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/example_easy\" target=\"_blank\" rel=\"noopener\">example_easy\/<\/a><\/strong> folder.<\/p>\n<h2>Testing the server<\/h2>\n<p>If you do not have a REST client, the server can be tested on any web browser by adding the <strong>debug=1<\/strong> parameter to the URL as well as the <strong>method<\/strong> parameter with the method desired (not needed if the method is <strong>GET<\/strong>), as for example: <em>http:\/\/localhost\/fake_rest_server\/src\/index.php\/route_1\/subroute?method=post&amp;debug=1&amp;username=Joan<\/em><\/p>\n<p>Following the <a href=\"#example\">example above<\/a>, you can use a web browser to visit the following links:<\/p>\n<p><em>http:\/\/localhost\/route_to_the_REST_server\/index.php\/myRESTService\/user\/?method=get&amp;debug=1&amp;id=1<\/em> (it should show &#8220;John Doe likes eating meat&#8221;)<\/p>\n<p><em>http:\/\/localhost\/route_to_the_REST_server\/index.php\/myRESTService\/user\/?method=get&amp;debug=1&amp;id=2<\/em> (it should show &#8220;Joan Alba Maldonado likes eating pizza&#8221;)<\/p>\n<p><em>http:\/\/localhost\/route_to_the_REST_server\/index.php\/myRESTService\/user\/?method=get&amp;debug=1&amp;id=3<\/em> (it should show &#8220;User cannot be found! (id=3)&#8221;)<\/p>\n<p><em>http:\/\/localhost\/route_to_the_REST_server\/index.php\/myRESTService\/user\/?method=get&amp;debug=1<\/em> (it should show &#8220;No id sent!&#8221;)<\/p>\n<p>Note that the &#8220;<em>\/<\/em>&#8221; character at the end of the route is optional.<\/p>\n<h2>Final comments<\/h2>\n<p>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 <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/example\" target=\"_blank\" rel=\"noopener\">example\/<\/a><\/strong> and the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/example_easy\" target=\"_blank\" rel=\"noopener\">example_easy\/<\/a><\/strong> folder) but they can be deleted.<\/p>\n<p>The only really-needed code is located in the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/\" target=\"_blank\" rel=\"noopener\">src\/<\/a><\/strong> folder. Inside of it, the <strong><a href=\"https:\/\/github.com\/jalbam\/fake_rest_server\/blob\/master\/src\/route_1\" target=\"_blank\" rel=\"noopener\">route_1\/<\/a><\/strong> folder and all of its content can also be deleted since it is just an example.<\/p>\n<h2>License<\/h2>\n<p>This project can be used, reproduced, distributed and modified freely for any non-commercial purposes but always keeping the author&#8217;s name and copyright clauses. Other than that, just use this project as you wish but never sell it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;fake_rest_server&#8221; 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 was made on 12th May 2016 (approximately). It can be found on GitHub: https:\/\/github.com\/jalbam\/fake_rest_server Configuring the server To add new paths (routes) and methods, the developer just needs to &hellip; <a href=\"https:\/\/joanalbamaldonado.com\/blog\/2018\/11\/30\/fake_rest_server-rest-server-in-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;fake_rest_server &#8211; REST server in PHP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,97,49,9,104,105,74,75],"tags":[56,102,60,24,106,107,77,76],"class_list":["post-157","post","type-post","status-publish","format-standard","hentry","category-development","category-library","category-php","category-programming","category-rest","category-server","category-tool","category-utility","tag-development","tag-library","tag-php","tag-programming","tag-rest","tag-server","tag-tool","tag-utility"],"_links":{"self":[{"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":18,"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions\/180"}],"wp:attachment":[{"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joanalbamaldonado.com\/blog\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}