{"flag":true,"single":true,"pageTitle":"PHP REST Api structure","post":{"id":299,"user_id":"1","slug":"php-rest-api-structure-3dhn","title":"PHP REST Api structure","body":"<p>ApiHandler.php<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\nclass APIHandler {\r\n    public function processRequest() {\r\n        $method = $_SERVER['REQUEST_METHOD'];\r\n        $uri = explode('\/', trim($_SERVER['REQUEST_URI'], '\/'));\r\n\r\n        \/\/ Get action (e.g., users, funds, history)\r\n        $resource = $uri[1] ?? '';\r\n        $action = $uri[2] ?? '';\r\n        \r\n        \/\/ Map routes to methods\r\n        if ($resource === 'api') {\r\n            switch ($method) {\r\n                case 'GET':\r\n                    if ($action === 'users') return $this-&gt;getUsers();\r\n                    if ($action === 'funds') return $this-&gt;getFunds();\r\n                    if ($action === 'history') return $this-&gt;getHistory();\r\n                    break;\r\n\r\n                case 'POST':\r\n                    if ($action === 'users') return $this-&gt;createUser();\r\n                    break;\r\n\r\n                default:\r\n                    return $this-&gt;response(405, ['error' =&gt; 'Method Not Allowed']);\r\n            }\r\n        }\r\n\r\n        return $this-&gt;response(404, ['error' =&gt; 'Not Found']);\r\n    }\r\n\r\n    private function getUsers() {\r\n        return $this-&gt;response(200, [\r\n            ['id' =&gt; 1, 'name' =&gt; 'Alice'],\r\n            ['id' =&gt; 2, 'name' =&gt; 'Bob']\r\n        ]);\r\n    }\r\n\r\n    private function getFunds() {\r\n        return $this-&gt;response(200, [\r\n            'total' =&gt; 5000,\r\n            'available' =&gt; 4200\r\n        ]);\r\n    }\r\n\r\n    private function getHistory() {\r\n        return $this-&gt;response(200, [\r\n            ['action' =&gt; 'deposit', 'amount' =&gt; 1000],\r\n            ['action' =&gt; 'withdrawal', 'amount' =&gt; 200]\r\n        ]);\r\n    }\r\n\r\n    private function createUser() {\r\n        $input = json_decode(file_get_contents(\"php:\/\/input\"), true);\r\n        if (!isset($input['name'])) {\r\n            return $this-&gt;response(400, ['error' =&gt; 'Name is required']);\r\n        }\r\n\r\n        return $this-&gt;response(201, [\r\n            'id' =&gt; rand(1000, 9999),\r\n            'name' =&gt; $input['name']\r\n        ]);\r\n    }\r\n\r\n    private function response($status, $data) {\r\n        http_response_code($status);\r\n        header('Content-Type: application\/json');\r\n        echo json_encode($data);\r\n    }\r\n}\r\n<\/code><\/pre>\r\n<p>index.php<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\nrequire_once 'APIHandler.php';\r\n\r\n$api = new APIHandler();\r\n$api-&gt;processRequest();\r\n<\/code><\/pre>\r\n<p>.htaccess<\/p>\r\n<pre class=\"language-markup\"><code>RewriteEngine On\r\nRewriteBase \/api\/\r\nRewriteCond %{REQUEST_FILENAME} !-f\r\nRewriteCond %{REQUEST_FILENAME} !-d\r\nRewriteRule ^(.*)$ index.php [QSA,L]<\/code><\/pre>\r\n<p>Now visit:<br><br>http:\/\/localhost\/project\/api\/funds<\/p>\r\n<p>&nbsp;<\/p>","category_id":"1","is_private":"0","created_at":"2025-04-23T05:49:48.000000Z","updated_at":"2025-04-23T05:49:48.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"ApiHandler.php &lt;?php class APIHandler {     public function processRequest() {         $method = $_SERVER['REQUEST_METHOD'];         $uri - PHP REST Api structure (Updated: April 23, 2025) - Read more about PHP REST Api structure at my programming site [SITE]","categories":[]}