Skip to content

Instantly share code, notes, and snippets.

@azazqadir azazqadir/readme.md
Created Oct 27, 2018

Embed
What would you like to do?
Parsing Data in JSON in Codeigniter

You can use Codeigniter curl library to parse data in JSON format. For this you can use POST request.

Add this to your JSON file

$this->load->library('Curl');

$url = 'http://your-domin.com/';

$formatJsondata = array(

   'username' => 'authenticate-username',

   'password' => 'password-authentication'

);

$encodedJsonData = json_encode($formatJsondata);

$this->curl->create($url);

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

$this->curl->post($encodedJsonData);

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

To show data, add this to config.php file

Class MY_Loader extends CI_Loader {

public $_json_array = array();

public function __construct() {

parent::__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;

$this->ci->output

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

}

else {

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

}

}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.