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.

How To Display Data in Codeigniter JSON Format Using CURL Operations

Updated on May 27, 2021

4 Min Read
codeigniter json

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.

Laravel JSON could be a little package that produces encoding and interpreting JSON in a breeze with exceptions thrown on error instantly: A basic wrapper around json_encode() and json_decode() for catching any mistakes without executing json_last_error().

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 in applications deployed on any web hosting with PHP and MySQL support.

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


$url = '';

$formatJsondata = array(

   'username' => 'authenticate-username',

   'password' => 'password-authentication'


$encodedJsonData = json_encode($formatJsondata);


$this->curl->option(CURLOPT_HTTPHEADER, array('Content-type: application/json; Charset=UTF-8'));


echo $result = $this->curl->execute();

$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

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

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.

Class MY_Loader extends CI_Loader {

public $_json_array = array();

public function __construct() {


$this->ci = &get_instance();


public function view($view, $vars = array(), $return = FALSE) {

$format = $this->ci->input->get('format');

if($format == 'json') {

$this->ci->_json_array[$view] = $vars;


->set_output(json_encode($this->ci->_json_array, JSON_NUMERIC_CHECK));


else {

parent::view($view, $vars, $return);




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:

defined('BASEPATH') OR exit('No direct script access allowed');

class Formatting


function __construct()


$this->ci = &get_instance();


function convert_html_to_json()




echo $this->ci->output->get_output();




echo NULL;




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

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

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 [email protected]


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

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Unleash The Cloud.
Not Budgets.

For 4 Months +
Up To 30 Free Migrations

Cyber Week

Time Left In Offer
  • 0


  • 0


  • 0


  • 0


40% OFF

On All Plans