What is JSON ? How to Fetch, Decode & Handle JSON data from an API using PHP ? [WordPress API as example]

In this tutorial, I will teach you and help you to understand that how to handle JSON Data from an API in PHP environment.

What is JSON ? How to Fetch, Decode & Handle JSON data from an API using PHP ? [WordPress JSON API as example]

What is JSON ? How to Fetch, Decode & Handle JSON data from an API using PHP ? [WordPress JSON API as example]

I have used WordPress JSON API as an example to help you understand it much better. Also, you can use the code to develop an Application with WordPress JSON API (JetPack Plugin is required)

What is JSON ?

First of all, Let me tell you what is JSON.
JSON stands from JavaScript Object Notation. It is lightweight and widely used for data-interchange format.
One can easily convert and array into JSON data for API purpose. humans can easily read and write data in JSON format. Also for machines, It is easy to parse and generate data in JSON format from any other format such as array.

Main use of JSON (API) ?

There are many websites on the Internet who offers API for providing there private data present in their secured database to general public for Application Programming purpose. API stands for Application Programming Interface which provide an interface between any two servers for exchanging secured data without giving direct access to their secured databases.

Now, I am going to start the Tutorial, The Tutorial will be for PHP environment.

How to Fetch JSON Data from an API ?

You can fetch JSON Data from an API by using file_get_contents(); function which is already available in PHP :-

I am taking WordPress JSON API available through JetPack as an example in this tutorial as given below

API: https://public-api.wordpress.com/rest/v1.1/sites/blog.thesmartware.com/posts/536

This API link will return data of this Post (ID: 536) in JSON format, You can visit it to see the JSON response live in you web browser or alternatively you can use WordPress Developer Console: https://developer.wordpress.com/docs/api/console/

How to Decode JSON data from an API ?

The data stored in variable $JSON which is shown below, just for more understanding. This is the actual data in JSON format returned by the API server over the API URL link to the requesting server.

(Snippet given below is just for better understanding)

Decoding the JSON format using json_decode()  function to convert the JSON formatted data into an Array (or Group of Arrays)

The Array you will get after above code is executed will be stored & available in variable $Responce

You can check the values stored in variable $Responce using print_r() function which will show you the structure of Array, it will help you in understanding & handling of Array.

(Snippet given below is just for better understanding)

How to Handle JSON Data from an API ?

The Final Code will look like (Complete Code) :-

The Final OutPut will look like :-

Post Title: What is JSON ? How to Fetch, Decode & Handle JSON data from an API using PHP ? [WordPress JSON API as example]
Post URL: http://magzinly.com/what-is-json-how-to-fetch-decode-handle-json-data-from-an-api-using-php-wordpress-json-api-as-example
Post Excerpt: In this tutorial, I will teach you and help you to understand that how to handle JSON Data from an API in PHP environment. I…

Author Login: erbhaveshkumaar123
Comment Count: 0


Please feel free to ask any question or give suggestions or if you need any help either by commenting below or at our 1Ask (Q&A) and 1Forum

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

erbhaveshkumar123

I am a Person who Loves to Do Innovative things.

You may also like...

1 Response

  1. Please feel free to ask any question or give suggestions or if you need any help either by commenting below or at our 1Ask (Q&A) and 1Forum

Follow

Get the latest posts delivered to your mailbox: