Simple API using PHP with JSON response

In this post, we will see how to make a simple API using PHP with JSON response. Basically, this API will take parameters via the GET method, and then returns its response in JSON Object.  JSON is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.  A Simple API using PHP with JSON response can be used to send and receive data to mobile apps as well as web apps or websites.

It’s a best practice to create an API consisting of these 3 files:

  1. config.php [The Database Configuration File]
  2. myClass.php [The Class containing functions, for modularity]
  3. index.php [The Handler, which invokes respective functions, on GET Requests]

Follow the below steps to create a Simple API using PHP with JSON response.

Step 1: Let’s create the config.php file

Step 2: It’s the myClass.php file, it consists all the functions needed in the API, in this simple API using PHP with JSON response, we will be having functions like loginCheck, register, showDetails.

Step3: Now let us create the driver file, index.php, which calls respective functions of myClass, according to the parameters received via the GET method, i.e. from the URL.

Well, congrats, you just made your own PHP API, now you can use it either in your Android apps, or web apps, or even websites. If this post was helpful, take a moment to share this with your friends, and help us to grow!

Hey there, I’m Sid. I often write articles here. Apart from being a developer, my interest range from blogging, designing, tinkering and hearing electronic. To connect with me, just navigate to any social icons, below!

Sidharth Patnaik

Hey there, I'm Sid. I often write articles here. Apart from being a developer, my interest range from blogging, designing, tinkering and hearing electronic. To connect with me, just navigate to any social icons, below!

Leave a Reply

Your email address will not be published. Required fields are marked *