Jan 29, 2026
## Chapter 22: A tiny API endpoint (clean JSON response)
Goal of this chapter: return JSON properly with correct headers.
Put this in `public/api_time.php` and run the built-in server:
```php
<?php
header("Content-Type: application/json; charset=utf-8");
echo json_encode([
"ok" => true,
"server_time" => date("c"),
]);
```
Open:
- http://localhost:8000/api_time.php
This is the simplest “API” shape: correct header + JSON output.
---
### One-file routing (how small PHP apps avoid 50 separate files)
Many real apps don’t create a new `.php` file for every URL. They keep one entry file (often `public/index.php`) and route requests by path.
Create `public/index.php`:
```php
<?php
declare(strict_types=1);
$path = parse_url($_SERVER["REQUEST_URI"] ?? "/", PHP_URL_PATH) ?? "/";
$method = $_SERVER["REQUEST_METHOD"] ?? "GET";
if ($path === "/" && $method === "GET") {
header("Content-Type: text/html; charset=utf-8");
echo "
<h1>Home</h1>
";
exit;
}
if ($path === "/api/time" && $method === "GET") {
header("Content-Type: application/json; charset=utf-8");
echo json_encode(["now" => date("c")]);
exit;
}
http_response_code(404);
header("Content-Type: text/plain; charset=utf-8");
echo "Not found";
```
Run:
```bash
php -S localhost:8000 -t public
```
Try:
- http://localhost:8000/
- http://localhost:8000/api/time
This is the basic shape that later grows into proper routers and frameworks.
---
## Conclusion
In this article, we explored the core concepts of All about PHP - A tiny API endpoint (clean JSON response). Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is All about PHP - A tiny API endpoint (clean JSON response)?**
A: All about PHP - A tiny API endpoint (clean JSON response) is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is All about PHP - A tiny API endpoint (clean JSON response) important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with All about PHP - A tiny API endpoint (clean JSON response)?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for All about PHP - A tiny API endpoint (clean JSON response)?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can All about PHP - A tiny API endpoint (clean JSON response) be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of All about PHP - A tiny API endpoint (clean JSON response)?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is All about PHP - A tiny API endpoint (clean JSON response) suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does All about PHP - A tiny API endpoint (clean JSON response) improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using All about PHP - A tiny API endpoint (clean JSON response)?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced All about PHP - A tiny API endpoint (clean JSON response)?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.