This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How To Display Data in Codeigniter JSON Format Using CURL Operations

Updated on  5th October

4 Min Read
codeigniter json
Reading Time: 4 minutes

JSON stands for JavaScript Object Notation and it is used for structuring data in a much more organized format. As an alternative to XML, the major purpose of using Codeigniter JSON is that it transmits data between web server and application.

It is a schema-less text based representation of data enabling us to access service information in human readable format. It is widely used in REST API for transferring data between the server and client, as the response is received in JSON about the APIs web services.

While at some points using Codeigniter CURL operations, developers extract web data in JSON format to get bunch of records in array format.

You Might Also Like: How to Create REST API in Codeigniter

JSON is primarily based on two parts i.e keys and values. Together they form a duo called key/value pair. Defining keys, it is a string which is enclosed in quotation marks. While the values could be either string, number, array, Boolean expression or any object. Together they form a specific syntax, in which key is followed by a colon and a value.

Though JSON comes from the JavaScript, but it is supported by almost all programming languages either natively or by using custom libraries.

Detailing its support in databases, JSON is supported by many relational databases including MySQL, PostgreSQL, MariaDB and others.

While NoSQL databases like MongoDB, Neo4J, HBASE also supports JSON but in a slight different manner. As MongoDB uses a modified binary version of JSON for data transferring process.

Display Data in JSON Using Codeigniter CURL Library

In this article, I will demonstrate how to send JSON responses via post using the Codeigniter CURL library. The main purpose of using this library is that it helps handling the CURL requests effectively.

First, you have to install the CURL library from the github repository of Codeigniter-cURL and place it inside the /application/libraries/Curl.php. It is a typical Codeigniter library which makes easy for the developers to perform CURL operations.

After completing the installation, load the CURL library by using the following command:

Once the Codeigniter CURL library is successfully loaded, set the content type of request to application/json. While doing this, an important thing to keep in mind here is that always use “application/json” and do not get wrecked by mistakenly using “text/json” ever. As simply using “text/json” is bit incorrect and could de-track the overall process.

You Might Also Like: Executing CRUD in PHP and MySQL

JSON Notation Send Through Post

$this->load->library(‘Curl’) →  load the Curl library

$formatJsondata = array() →  The JSON data

$encodedJsonData = json_encode($formatJsondata) → Encode the array into JSON

CURLOPT_HTTPHEADER →  set custom Http header and pass pointer to linked list to pass server and proxy Http request.

$this->curl->post($encodedJsonData) →   Post – If you do not use post, it will just run a GET request

execute() → return response in $result

Technique to Show Data in JSON

Sometimes converting an entire HTML page to JSON format is necessary. As you are required to use that data in particular scripts for specified operations. Loading the data output of the page in JSON format mostly happens when the query is specifically requested for the data rendered on the page or if it is needed to pass the dynamic data to the view from controller.

You Might Also Like: Create Basic CRUD in Codeigniter

So to load the page data in Codeigniter JSON format, first create a custom class with the name MY_Loader inside the application/core. The core folder of the application must contain all the extended classes of core inside it. While the class name should also be given same as the core class with prefix set in config_php. Like for my extended classes, I have set the prefix as ‘MY_’ ( $config[‘subclass_prefix’] = ‘MY’ ). You can check your prefix in config.php of your application.

The output array sets up completely with all the parameters of the page, once all the views of the home page gets loaded.

Now we are required to display the output in JSON format by using the display_override hook of Codeigniter controller. In this case, you can also use your own method to display the output on web or could also send the JSON data to the API which has requested for it.

In order to display output in Codeigniter JSON format on the web, open hooks.php and inside it create a hook on display override as:

Final Words

So in this article, I have demonstrated in detail how to display page data in typical JSON format using Codeigniter CURL operations. JSON is primarily used as an alternative to XML for structuring and transferring data between clients and web servers. Using CURL library of Codeigniter, you can convert an entire HTML page into desired JSON format. If you further want to contribute your thoughts about how to get JSON data in Codeigniter or have any other query regarding this article, you can write your comments below in the comments section.

Share your opinion in the comment section. COMMENT NOW

Pardeep Kumar

Pardeep is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He love to work on Open source platform , Frameworks and working on new ideas. You can email him at pardeep.kumar@cloudways.com

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.