@shyann
To post data as JSON in CodeIgniter, you can use the following steps:
1 2 3 4 5 6 7 |
$data = array( 'key1' => 'value1', 'key2' => 'value2' ); $json_data = json_encode($data); echo $json_data; |
1 2 3 4 5 6 7 |
$data = array( 'key1' => 'value1', 'key2' => 'value2' ); $this->output->set_content_type('application/json'); echo json_encode($data); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$data = array( 'key1' => 'value1', 'key2' => 'value2' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.com/api/endpoint'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo $response; |
These are some ways you can post data as JSON in CodeIgniter. Choose the method that best fits your requirements and implement it in your application.