Creating blog post endpoints

Till now, we are done with most of the common code. So let's look at blog post endpoints. In blog post endpoints, the first one is the blog posts listing.

Blog posts listing endpoints:

  • URI: /api/posts
  • Method: GET

So, let's replace the previous code in posts.php with the proper code to serve posts. And to serve this, put the following code in the posts.php file:

<?php$url = $_SERVER['REQUEST_URI'];// checking if slash is first character in route otherwise add itif(strpos($url,"/") !== 0){    $url = "/$url";}if($url == '/posts' && $_SERVER['REQUEST_METHOD'] == 'GET') {    $posts = getAllPosts();    echo json_encode($posts);}function getAllPosts() {    return [        [            'id' => 1,            'title' => 'First Post',            'content' => 'It is all ...

Get Building RESTful Web Services with PHP 7 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.