How to send custom HTTP header in response?
The problem is that some of your $_GET
variables aren't set, this will throw an error (the extra output text you have) and can be prevent by checking if they are empty first before using them.
$request = array( 'request' => !empty($_GET['request']) ? $_GET['request'] : '', 'device_id' => !empty($_GET['device_id']) ? $_GET['device_id'] : '', 'launch_date'=> !empty($_GET['launch_date']) ? $_GET['launch_date'] : '', 'allowed_hours'=> !empty($_GET['allowed_hours']) ? $_GET['allowed_hours'] : '', 'site_id'=> !empty($_GET['site_id']) ? $_GET['site_id'] : '', 'product'=> !empty($_GET['product']) ? $_GET['product'] : '', 'software_version'=> !empty($_GET['software_version']) ? $_GET['software_version'] : '', 'platform_os'=> !empty($_GET['platform_os']) ? $_GET['platform_os'] : '', 'platform'=> !empty($_GET['platform']) ? $_GET['platform'] : '', 'platform_model'=> !empty($_GET['platform_model']) ? $_GET['platform_model'] : '');$response = array( 'response_code' =>200 , 'device_id'=> !empty($_GET['device_id']) ? $_GET['device_id'] : '', 'allowed_hours'=> !empty($_GET['allowed_hours']) ? $_GET['allowed_hours'] : '', 'product'=>'mlc', 'prov_ur'=>NULL );header('Content-Type: application/json');echo json_encode( $response );
Using the normal php get like below will sometimes throw some errors
$_GET['allowed_hours'];
I would recommend change to codeIgniter input class methods.
$this->input->get('allowed_hours');
Codeigniter Input Class will save you putting lot of code.
As said in user guide: With CodeIgniter’s built in methods you can simply do this:
Updated:
$request = array( 'request' => $this->input->get('request'), 'device_id' => $this->input->get('device_id'), 'launch_date'=> $this->input->get('launch_date'), 'allowed_hours'=> $this->input->get('allowed_hours'), 'site_id'=> $this->input->get('site_id'), 'product'=>$this->input->get('product'), 'software_version'=> $this->input->get('software_version'), 'platform_os'=> $this->input->get('platform_os'), 'platform'=> $this->input->get('platform'), 'platform_model'=> $this->input->get('platform_model'));
Codeigniter has own Output Class
And
$response = array( 'response_code' => 200, 'device_id'=> $this->input->get('device_id'), 'allowed_hours'=> $this->input->get('allowed_hours'), 'product'=> 'mlc', 'prov_ur'=> NULL );return $this->output ->set_content_type('Content-Type: application/json') ->set_output(json_encode($response));