php – 如何发送自定义HTTP标头作为响应?
我想在我的HTTP标头中发送json数据.
我正在使用Codeigniter PHP,所以我在我的控制器中执行了此操作: header('Content-Type: application/json');' 这是我的代码: $request = array( 'request' => $_GET['request'],'device_id' => $_GET['device_id'],'launch_date'=> $_GET['launch_date'],'allowed_hours'=>$_GET['allowed_hours'],'site_id'=>$_GET['site_id'],'product'=>$_GET['product'],'software_version'=>$_GET['software_version'],'platform_os'=>$_GET['platform_os'],'platform'=>$_GET['platform'],'platform_model'=>$_GET['platform_model'] ); $response = array( 'response_code' =>200,'device_id'=>$_GET['device_id'],'product'=>'mlc','prov_ur'=>NULL ); header('Content-Type: application/json'); echo json_encode( $response ); 但是当我打印我的标题时,我得到了
{"response_code":200,"device_id":"70D0D01FBAD2","allowed_hours":null,"product":"mlc","prov_ur":null} 在我的回复中.我不想在我的HTTP标头响应中发送其他数据. 根据CI提升代码 public function index() { $request = array( 'request' => $this->get('request'),'device_id' => $this->get('device_id'),'launch_date'=> $this->get('launch_date'),'allowed_hours'=>$this->get('allowed_hours'),'site_id'=> $this->get('site_id'),'product'=>$this->get('product'),'software_version'=> $this->get('software_version'),'platform_os'=> $this->get('platform_os'),'platform'=> $this->get('platform'),'platform_model'=> $this->get('platform_model') ); $response = array( 'response_code' =>200,'device_id'=> $this->get('device_id'),'allowed_hours'=> $this->get('allowed_hours'),'prov_ur'=>NULL ); $this->output->set_content_type('Content-Type: application/json'); return $this->output ->set_content_type('Content-Type: application/json') ->set_output(json_encode($response)); echo $response; } 解决方法
使用正常的php得到如下有时会抛出一些错误
$_GET['allowed_hours']; 我建议更改为codeIgniter输入类方法. $this->input->get('allowed_hours'); Codeigniter Input Class将为您节省大量代码. 如用户指南中所述:使用CodeIgniter的内置方法,您可以简单地执行此操作: 更新: $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拥有自己的Output Class 和 $response = array( 'response_code' => 200,'device_id'=> $this->input->get('device_id'),'product'=> 'mlc','prov_ur'=> NULL ); return $this->output ->set_content_type('Content-Type: application/json') ->set_output(json_encode($response)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |