Book Image

Learning Phalcon PHP

Book Image

Learning Phalcon PHP

Overview of this book

Table of Contents (17 chapters)
Learning Phalcon PHP
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using APIs – recommended practices


If you are completely new to APIs, I recommend that you read at least the basics about developing an API. In the simplest way, an API response can be created with plain PHP, like this:

$data = [
  'name' => 'John Doe',
  'age' => 50
];

echo json_encode($data);

Next, we are going to talk about some general rules that you should follow when developing an API, which are discussed as follows:

  • Use plural nouns instead of verbs, use concrete names, and make use of HTTP verbs (GET, POST, PUT, and DELETE) to operate on them:

    This format is bad:

    GET /getAllArticles
    GET /getArticle
    POST /newArticle

    This format is good:

    GET /articles (Retrieve all articles)
    GET /article/12 (Retrieve article with id 12)
    POST /article (Create a new article)
    PUT /article/12 (Update article with id 12)
    DELETE /article/12 (Delete article with id 12)
  • Use verbs when the response does not involve a resource:

    GET /search?title=Learning+Phalcon

    Note

    Always version your API. In this way, when you...