{"id":1075,"date":"2024-08-30T07:50:47","date_gmt":"2024-08-30T07:50:47","guid":{"rendered":"https:\/\/blog.200oksolutions.com\/?p=1075"},"modified":"2026-11-20T11:03:31","modified_gmt":"2026-11-20T11:03:31","slug":"lightweight-php-restful-api","status":"publish","type":"post","link":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/","title":{"rendered":"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Build Your Own Lightweight PHP RESTful API   landscape\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/rWuvqxcjuVQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Most PHP tutorials push you toward Laravel or Symfony the moment you want to make a REST API in PHP. That is fine for large projects, but for a small service, a prototype or a mobile app backend, a framework can be more weight than you need.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide you will create a simple REST API using basic PHP, with no framework and no Composer packages. You will build the four REST API methods (GET, POST, PUT and DELETE), protect them with token authentication, test everything with cURL and Postman, and then learn how to consume the API as a REST web service from another PHP application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to RESTful APIs in Core PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">RESTful APIs let different software systems talk to each other over HTTP. A mobile app, a website or another server sends a request to a URL, and the API answers with data, usually JSON. Each URL represents a resource (for example, a user), and the HTTP method says what to do with it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Frameworks make this easier, but they also add dependencies, configuration and overhead. When you only need a handful of endpoints, using Core PHP to build a lightweight RESTful API is practical and efficient.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why Choose Core PHP for Your RESTful API?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Minimal dependencies:<\/strong> Nothing to install or keep updated apart from PHP itself.<\/li>\n\n\n\n<li><strong>Lightweight and fast:<\/strong> Less code runs per request, so responses are quick and hosting needs are small.<\/li>\n\n\n\n<li><strong>Full control:<\/strong> You decide the structure, the routing and the response format.<\/li>\n\n\n\n<li><strong>Easy to learn from:<\/strong> Writing it by hand shows you exactly how REST, HTTP status codes and headers work, which carries over to any framework later.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Project Setup<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the directory structure we will use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/my-rest-api\n    \/api\n        \/v1\n            - index.php\n    \/data\n        - users.json\n    - .htaccess<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>api\/v1\/index.php:<\/strong> The single script that handles every API request.<\/li>\n\n\n\n<li><strong>data\/users.json:<\/strong> A JSON file that acts as a simple database for this example.<\/li>\n\n\n\n<li><strong>.htaccess:<\/strong> Rewrites clean URLs to our script and passes the Authorization header to PHP.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Create <code>data\/users.json<\/code> with a couple of sample users:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n    { \"id\": 1, \"name\": \"Asha Patel\", \"email\": \"asha@example.com\" },\n    { \"id\": 2, \"name\": \"Ravi Shah\", \"email\": \"ravi@example.com\" }\n]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Routing with .htaccess<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This configuration sends every request under <code>\/api\/v1\/<\/code> to our PHP script. The first rule is needed for token authentication: Apache often strips the <code>Authorization<\/code> header before PHP can read it, and this line passes it through.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\n\n# Pass the Authorization header to PHP (needed for token authentication)\nRewriteRule .* - &#91;E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\n\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^api\/v1\/(.*)$ api\/v1\/index.php?request=$1 &#91;QSA,L]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note the flags <code>[QSA,L]<\/code>: QSA keeps any query string and L stops processing further rules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Core API Logic: Helpers and Token Authentication<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">All of the logic lives in <code>api\/v1\/index.php<\/code>. We start with a JSON response helper, the token check and two functions that read and write our JSON &#8220;database&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>A JSON Response Helper<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sending the right HTTP status code matters in a REST API. This small helper sets the status, prints JSON and stops the script, so we never forget either step:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nheader(\"Content-Type: application\/json\");\n\nfunction sendJson($data, int $status = 200): void {\n    http_response_code($status);\n    echo json_encode($data);\n    exit;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Add Token Authentication to Your PHP REST API<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A REST API with no authentication lets anyone read or change your data. The simplest fix in basic PHP is a bearer token: the client sends a secret token in the <code>Authorization<\/code> header, and the API rejects any request without a valid one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, generate a long random token once and keep it out of your code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -r \"echo bin2hex(random_bytes(32));\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Store it in an environment variable named <code>API_TOKEN<\/code> (for example with <code>SetEnv API_TOKEN \"your-token\"<\/code> in your Apache virtual host). Then add this check:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function requireToken(): void {\n    $header   = $_SERVER&#91;'HTTP_AUTHORIZATION'] ?? '';\n    $expected = getenv('API_TOKEN') ?: '';\n\n    if ($expected === ''\n        || !preg_match('\/^Bearer\\s+(.+)$\/i', $header, $m)\n        || !hash_equals($expected, $m&#91;1])) {\n        header('WWW-Authenticate: Bearer');\n        sendJson(&#91;\"message\" =&gt; \"Unauthorized\"], 401);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Three details are worth noting. <code>hash_equals()<\/code> compares strings in constant time, which avoids timing attacks. An empty expected token always fails, so a missing environment variable can never leave the API open. And a 401 response with the <code>WWW-Authenticate<\/code> header tells clients exactly what went wrong.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is the simplest form of token authentication. For per-user tokens, expiry or revocation, store tokens in a database table or move to JWT (JSON Web Tokens).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Utility Functions<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">These two functions read and write the users file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getUsers(): array {\n    $file = __DIR__ . '\/..\/..\/data\/users.json';\n    return json_decode(file_get_contents($file), true) ?? &#91;];\n}\n\nfunction saveUsers(array $users): void {\n    $file = __DIR__ . '\/..\/..\/data\/users.json';\n    file_put_contents($file, json_encode(array_values($users), JSON_PRETTY_PRINT), LOCK_EX);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>REST API Methods in PHP: GET, POST, PUT and DELETE<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The REST API methods map to CRUD operations on a resource:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>HTTP method<\/th><th>Action<\/th><th>Example endpoint<\/th><\/tr><tr><td>GET<\/td><td>Read one or all users<\/td><td><code>GET \/api\/v1\/<\/code> or <code>GET \/api\/v1\/1<\/code><\/td><\/tr><tr><td>POST<\/td><td>Create a user<\/td><td><code>POST \/api\/v1\/<\/code><\/td><\/tr><tr><td>PUT<\/td><td>Update a user<\/td><td><code>PUT \/api\/v1\/1<\/code><\/td><\/tr><tr><td>DELETE<\/td><td>Delete a user<\/td><td><code>DELETE \/api\/v1\/1<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>GET Request<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Return all users, or one user by ID. A missing user returns 404:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function handleGet(string $request): void {\n    $users = getUsers();\n\n    if ($request === '') {\n        sendJson($users);\n    }\n\n    $userId = (int) explode('\/', $request)&#91;0];\n    foreach ($users as $user) {\n        if ((int) $user&#91;'id'] === $userId) {\n            sendJson($user);\n        }\n    }\n    sendJson(&#91;\"message\" =&gt; \"User not found\"], 404);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>POST Request<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new user. We validate the input and return 201 (Created):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function handlePost(): void {\n    $input = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n    if (empty($input&#91;'name']) || empty($input&#91;'email'])) {\n        sendJson(&#91;\"message\" =&gt; \"name and email are required\"], 400);\n    }\n\n    $users   = getUsers();\n    $newId   = $users ? max(array_column($users, 'id')) + 1 : 1;\n    $newUser = &#91;\"id\" =&gt; $newId, \"name\" =&gt; $input&#91;'name'], \"email\" =&gt; $input&#91;'email']];\n\n    $users&#91;] = $newUser;\n    saveUsers($users);\n    sendJson($newUser, 201);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>PUT Request<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Update an existing user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function handlePut(string $request): void {\n    $input = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n    if (empty($input&#91;'name']) || empty($input&#91;'email'])) {\n        sendJson(&#91;\"message\" =&gt; \"name and email are required\"], 400);\n    }\n\n    $users  = getUsers();\n    $userId = (int) explode('\/', $request)&#91;0];\n\n    foreach ($users as $i =&gt; $user) {\n        if ((int) $user&#91;'id'] === $userId) {\n            $users&#91;$i]&#91;'name']  = $input&#91;'name'];\n            $users&#91;$i]&#91;'email'] = $input&#91;'email'];\n            saveUsers($users);\n            sendJson($users&#91;$i]);\n        }\n    }\n    sendJson(&#91;\"message\" =&gt; \"User not found\"], 404);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>DELETE Request<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Delete a user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function handleDelete(string $request): void {\n    $users  = getUsers();\n    $userId = (int) explode('\/', $request)&#91;0];\n\n    foreach ($users as $i =&gt; $user) {\n        if ((int) $user&#91;'id'] === $userId) {\n            unset($users&#91;$i]);\n            saveUsers($users);\n            sendJson(&#91;\"message\" =&gt; \"User deleted\"]);\n        }\n    }\n    sendJson(&#91;\"message\" =&gt; \"User not found\"], 404);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Routing the Request<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, authenticate the request, read the HTTP method and call the right handler. Put this at the bottom of <code>index.php<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>requireToken();\n\n$requestMethod = $_SERVER&#91;'REQUEST_METHOD'];\n$request       = trim($_GET&#91;'request'] ?? '', '\/');\n\nswitch ($requestMethod) {\n    case 'GET':\n        handleGet($request);\n        break;\n    case 'POST':\n        handlePost();\n        break;\n    case 'PUT':\n        handlePut($request);\n        break;\n    case 'DELETE':\n        handleDelete($request);\n        break;\n    default:\n        sendJson(&#91;\"message\" =&gt; \"Method Not Allowed\"], 405);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Testing the API<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Test with Postman or cURL. Replace <code>YOUR_TOKEN<\/code> with the token you generated. First, confirm that a request without a token is rejected:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -i http:\/\/your-domain.com\/api\/v1\/\n# HTTP\/1.1 401 Unauthorized<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Then try each method with the token:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Get all users:<\/strong> <code>curl -H \"Authorization: Bearer YOUR_TOKEN\" http:\/\/your-domain.com\/api\/v1\/<\/code><\/li>\n\n\n\n<li><strong>Get one user:<\/strong> <code>curl -H \"Authorization: Bearer YOUR_TOKEN\" http:\/\/your-domain.com\/api\/v1\/1<\/code><\/li>\n\n\n\n<li><strong>Create a user:<\/strong> <code>curl -X POST -H \"Authorization: Bearer YOUR_TOKEN\" -H \"Content-Type: application\/json\" -d '{\"name\":\"New User\",\"email\":\"new@example.com\"}' http:\/\/your-domain.com\/api\/v1\/<\/code><\/li>\n\n\n\n<li><strong>Update a user:<\/strong> <code>curl -X PUT -H \"Authorization: Bearer YOUR_TOKEN\" -H \"Content-Type: application\/json\" -d '{\"name\":\"Updated User\",\"email\":\"updated@example.com\"}' http:\/\/your-domain.com\/api\/v1\/1<\/code><\/li>\n\n\n\n<li><strong>Delete a user:<\/strong> <code>curl -X DELETE -H \"Authorization: Bearer YOUR_TOKEN\" http:\/\/your-domain.com\/api\/v1\/1<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In Postman, set the Authorization tab to &#8220;Bearer Token&#8221; and paste your token once for the whole collection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Consume a REST Web Service in PHP with cURL<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Building the API is half the job. Any other PHP application, such as a website, a cron script or another backend, can consume it as a REST web service. cURL ships with PHP and works the same way for any REST API:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ch = curl_init('https:\/\/your-domain.com\/api\/v1\/1');\ncurl_setopt_array($ch, &#91;\n    CURLOPT_RETURNTRANSFER =&gt; true,\n    CURLOPT_TIMEOUT        =&gt; 10,\n    CURLOPT_HTTPHEADER     =&gt; &#91;\n        'Accept: application\/json',\n        'Authorization: Bearer YOUR_TOKEN',\n    ],\n]);\n\n$body   = curl_exec($ch);\n$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);\ncurl_close($ch);\n\n$user = ($status === 200) ? json_decode($body, true) : null;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To send data with POST, add these options before <code>curl_exec()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl_setopt($ch, CURLOPT_POST, true);\ncurl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(&#91;\n    'name'  =&gt; 'New User',\n    'email' =&gt; 'new@example.com',\n]));\n\/\/ also add 'Content-Type: application\/json' to CURLOPT_HTTPHEADER<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Two habits make consuming a REST web service reliable. Always check the HTTP status code before you decode the body, and always set a timeout so a slow service cannot hang your page. For a quick GET with no headers, <code>file_get_contents()<\/code> also works, but cURL gives you far more control.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices and Next Steps<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use HTTPS<\/strong> so the token and data are encrypted in transit. A bearer token over plain HTTP can be stolen.<\/li>\n\n\n\n<li><strong>Validate every input<\/strong> and return clear 400-level errors, as the POST and PUT handlers above do.<\/li>\n\n\n\n<li><strong>Replace the JSON file with MySQL<\/strong> for real projects, using PDO with prepared statements.<\/li>\n\n\n\n<li><strong>Version your API<\/strong> (<code>\/api\/v1\/<\/code>) so you can change it later without breaking existing clients.<\/li>\n\n\n\n<li><strong>Use proper status codes:<\/strong> 200 for success, 201 for created, 400 for bad input, 401 for missing or wrong token, 404 for not found and 405 for unsupported methods.<\/li>\n\n\n\n<li><strong>Rotate your token<\/strong> if it is ever exposed, and never commit it to Git.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You have now created a simple REST API using basic PHP: four REST API methods, token authentication, correct status codes and a working example of how to consume it as a REST web service. It stays lightweight because it uses nothing but Core PHP. From here, swap the JSON file for a database, add per-user tokens or JWT, and add more resources as your project grows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs about Creating a REST API in PHP<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. How do I create a simple API in PHP?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> Create an <code>index.php<\/code> that reads <code>$_SERVER['REQUEST_METHOD']<\/code>, routes on the URL, and returns <code>json_encode()<\/code> output with a JSON content-type header and the right status code. The steps above show a complete working version.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. What is a RESTful API in PHP?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> A RESTful API in PHP lets different systems communicate using standard HTTP methods such as GET, POST, PUT and DELETE. It performs CRUD operations on resources and usually returns JSON.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Which REST API methods can I use in PHP?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> GET reads a resource, POST creates one, PUT updates one and DELETE removes one. Return 405 (Method Not Allowed) for any method your API does not support.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Why should I use Core PHP for a REST API?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> Core PHP is lightweight, fast and needs no extra dependencies, which makes it a good fit for simple APIs where you want full control over the implementation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. How do I add token authentication to a basic PHP API?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> Read the bearer token from the <code>Authorization<\/code> header, compare it with your stored secret using <code>hash_equals()<\/code>, and return 401 if it does not match. See the token authentication section above for the full code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. How do I secure a REST API in Core PHP?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> Use token-based authentication, serve everything over HTTPS, validate all input and use prepared statements if you connect a database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Can I use other authentication methods besides a simple token?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> Yes. You can use Basic Auth, OAuth 2.0 or JWT (JSON Web Tokens). A static bearer token is the simplest option, while JWT or database-backed tokens suit multi-user APIs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. How do I consume a REST API in PHP?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> Use cURL (or <code>file_get_contents()<\/code> for very simple GET requests). Send the token in an <code>Authorization: Bearer<\/code> header, set a timeout, check the HTTP status code and then decode the JSON response.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. Can I integrate a database with this API?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A.<\/strong> Yes. Replace the JSON file functions with MySQL queries using PDO and prepared statements. The routing and handler structure stays the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. What are the best practices for REST API development?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A. <\/strong>Use correct HTTP status codes, keep PUT and DELETE idempotent, version your API, validate inputs, use HTTPS and keep secrets out of your source code.<\/p>\n\n\n<div class=\"is-default-size wp-block-site-logo\"><a href=\"https:\/\/www.200oksolutions.com\/blog\/\" class=\"custom-logo-link light-mode-logo\" rel=\"home\"><img fetchpriority=\"high\" decoding=\"async\" width=\"484\" height=\"191\" src=\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2026\/01\/cropped-200ok_logo.png\" class=\"custom-logo\" alt=\"200OK Solutions Blog\" srcset=\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2026\/01\/cropped-200ok_logo.png 484w, https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2026\/01\/cropped-200ok_logo-300x118.png 300w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Most PHP tutorials push you toward Laravel or Symfony the moment you want to make&hellip;<\/p>\n","protected":false},"author":5,"featured_media":5565,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[158,1],"tags":[286,36,280,282,283,285,281,284],"class_list":["post-1075","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backend","category-php","tag-api-tutorial","tag-backend-development","tag-core-php","tag-lightweight-php-api","tag-php-api-development","tag-php-programming","tag-restful-api","tag-simple-rest-api"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide<\/title>\n<meta name=\"description\" content=\"Create a simple REST API using basic PHP: GET, POST, PUT and DELETE methods, token authentication, and how to consume it from another PHP app.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide\" \/>\n<meta property=\"og:description\" content=\"Create a simple REST API using basic PHP: GET, POST, PUT and DELETE methods, token authentication, and how to consume it from another PHP app.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/\" \/>\n<meta property=\"og:site_name\" content=\"200OK Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-30T07:50:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-20T11:03:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2240\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Piyush Solanki\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Piyush Solanki\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/\"},\"author\":{\"name\":\"Piyush Solanki\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/person\/e07f6b8e3c9a90ce7b3b09427d26155e\"},\"headline\":\"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide\",\"datePublished\":\"2024-08-30T07:50:47+00:00\",\"dateModified\":\"2026-11-20T11:03:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/\"},\"wordCount\":1456,\"publisher\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png\",\"keywords\":[\"API Tutorial\",\"Backend Development\",\"Core PHP\",\"Lightweight PHP API\",\"PHP API Development\",\"PHP Programming\",\"RESTful API\",\"Simple REST API\"],\"articleSection\":[\"Backend\",\"PHP\"],\"inLanguage\":\"en-US\"},{\"@type\":[\"WebPage\",\"SearchResultsPage\"],\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/\",\"url\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\",\"name\":\"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide\",\"isPartOf\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png\",\"datePublished\":\"2024-08-30T07:50:47+00:00\",\"dateModified\":\"2026-11-20T11:03:31+00:00\",\"description\":\"Create a simple REST API using basic PHP: GET, POST, PUT and DELETE methods, token authentication, and how to consume it from another PHP app.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage\",\"url\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png\",\"contentUrl\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png\",\"width\":2240,\"height\":1260,\"caption\":\"Lightweight PHP RESTful API in Core PHP showing laptop with programming code and API development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.200oksolutions.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Blog >Backend > Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#website\",\"url\":\"https:\/\/www.200oksolutions.com\/blog\/\",\"name\":\"Web Development, Software, and App Blog | 200OK Solutions\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.200oksolutions.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#organization\",\"name\":\"200OK Solutions\",\"url\":\"https:\/\/www.200oksolutions.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2025\/09\/200ok_logo-CGzMrWDu.png\",\"contentUrl\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2025\/09\/200ok_logo-CGzMrWDu.png\",\"width\":500,\"height\":191,\"caption\":\"200OK Solutions\"},\"image\":{\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.instagram.com\/200ok_solutions\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/person\/e07f6b8e3c9a90ce7b3b09427d26155e\",\"name\":\"Piyush Solanki\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2026\/05\/cropped-piyush-solanki-96x96.jpg\",\"contentUrl\":\"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2026\/05\/cropped-piyush-solanki-96x96.jpg\",\"caption\":\"Piyush Solanki\"},\"description\":\"Piyush is a seasoned PHP Tech Lead with 10+ years of experience architecting and delivering scalable web and mobile backend solutions for global brands and fast-growing SMEs. He specializes in PHP, MySQL, CodeIgniter, WordPress, and custom API development, helping businesses modernize legacy systems and launch secure, high-performance digital products. He collaborates closely with mobile teams building Android &amp; iOS apps , developing RESTful APIs, cloud integrations, and secure payment systems using platforms like Stripe, AWS S3, and OTP\/SMS gateways. His work extends across CMS customization, microservices-ready backend architectures, and smooth product deployments across Linux and cloud-based environments. Piyush also has a strong understanding of modern front-end technologies such as React and TypeScript, enabling him to contribute to full-stack development workflows and advanced admin panels. With a successful delivery track record in the UK market and experience building digital products for sectors like finance, hospitality, retail, consulting, and food services, Piyush is passionate about helping SMEs scale technology teams, improve operational efficiency, and accelerate innovation through backend excellence and digital tools.\",\"url\":\"https:\/\/www.200oksolutions.com\/blog\/author\/piyush\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide","description":"Create a simple REST API using basic PHP: GET, POST, PUT and DELETE methods, token authentication, and how to consume it from another PHP app.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/","og_locale":"en_US","og_type":"article","og_title":"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide","og_description":"Create a simple REST API using basic PHP: GET, POST, PUT and DELETE methods, token authentication, and how to consume it from another PHP app.","og_url":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/","og_site_name":"200OK Solutions Blog","article_published_time":"2024-08-30T07:50:47+00:00","article_modified_time":"2026-11-20T11:03:31+00:00","og_image":[{"width":2240,"height":1260,"url":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png","type":"image\/png"}],"author":"Piyush Solanki","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Piyush Solanki","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#article","isPartOf":{"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/"},"author":{"name":"Piyush Solanki","@id":"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/person\/e07f6b8e3c9a90ce7b3b09427d26155e"},"headline":"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide","datePublished":"2024-08-30T07:50:47+00:00","dateModified":"2026-11-20T11:03:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/"},"wordCount":1456,"publisher":{"@id":"https:\/\/www.200oksolutions.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage"},"thumbnailUrl":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png","keywords":["API Tutorial","Backend Development","Core PHP","Lightweight PHP API","PHP API Development","PHP Programming","RESTful API","Simple REST API"],"articleSection":["Backend","PHP"],"inLanguage":"en-US"},{"@type":["WebPage","SearchResultsPage"],"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api\/","url":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api","name":"Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide","isPartOf":{"@id":"https:\/\/www.200oksolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage"},"image":{"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage"},"thumbnailUrl":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png","datePublished":"2024-08-30T07:50:47+00:00","dateModified":"2026-11-20T11:03:31+00:00","description":"Create a simple REST API using basic PHP: GET, POST, PUT and DELETE methods, token authentication, and how to consume it from another PHP app.","breadcrumb":{"@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#primaryimage","url":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png","contentUrl":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2024\/08\/How-to-Migrate-to-Cloud-Native-Architectures-Using-Microservices-1-2026-11-20T163059.836.png","width":2240,"height":1260,"caption":"Lightweight PHP RESTful API in Core PHP showing laptop with programming code and API development"},{"@type":"BreadcrumbList","@id":"https:\/\/www.200oksolutions.com\/blog\/lightweight-php-restful-api#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.200oksolutions.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Blog >Backend > Lightweight PHP RESTful API in Core PHP: A Step-by-Step Guide"}]},{"@type":"WebSite","@id":"https:\/\/www.200oksolutions.com\/blog\/#website","url":"https:\/\/www.200oksolutions.com\/blog\/","name":"Web Development, Software, and App Blog | 200OK Solutions","description":"","publisher":{"@id":"https:\/\/www.200oksolutions.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.200oksolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.200oksolutions.com\/blog\/#organization","name":"200OK Solutions","url":"https:\/\/www.200oksolutions.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2025\/09\/200ok_logo-CGzMrWDu.png","contentUrl":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2025\/09\/200ok_logo-CGzMrWDu.png","width":500,"height":191,"caption":"200OK Solutions"},"image":{"@id":"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.instagram.com\/200ok_solutions\/"]},{"@type":"Person","@id":"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/person\/e07f6b8e3c9a90ce7b3b09427d26155e","name":"Piyush Solanki","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.200oksolutions.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2026\/05\/cropped-piyush-solanki-96x96.jpg","contentUrl":"https:\/\/www.200oksolutions.com\/blog\/wp-content\/uploads\/2026\/05\/cropped-piyush-solanki-96x96.jpg","caption":"Piyush Solanki"},"description":"Piyush is a seasoned PHP Tech Lead with 10+ years of experience architecting and delivering scalable web and mobile backend solutions for global brands and fast-growing SMEs. He specializes in PHP, MySQL, CodeIgniter, WordPress, and custom API development, helping businesses modernize legacy systems and launch secure, high-performance digital products. He collaborates closely with mobile teams building Android &amp; iOS apps , developing RESTful APIs, cloud integrations, and secure payment systems using platforms like Stripe, AWS S3, and OTP\/SMS gateways. His work extends across CMS customization, microservices-ready backend architectures, and smooth product deployments across Linux and cloud-based environments. Piyush also has a strong understanding of modern front-end technologies such as React and TypeScript, enabling him to contribute to full-stack development workflows and advanced admin panels. With a successful delivery track record in the UK market and experience building digital products for sectors like finance, hospitality, retail, consulting, and food services, Piyush is passionate about helping SMEs scale technology teams, improve operational efficiency, and accelerate innovation through backend excellence and digital tools.","url":"https:\/\/www.200oksolutions.com\/blog\/author\/piyush\/"}]}},"_links":{"self":[{"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1075"}],"version-history":[{"count":4,"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1075\/revisions"}],"predecessor-version":[{"id":5566,"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1075\/revisions\/5566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/media\/5565"}],"wp:attachment":[{"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.200oksolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}