Skip to content

Instantly share code, notes, and snippets.

@azazqadir
Created October 27, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azazqadir/26fd8d3b313c19cdb359908253d4966f to your computer and use it in GitHub Desktop.
Save azazqadir/26fd8d3b313c19cdb359908253d4966f to your computer and use it in GitHub Desktop.
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