如何发送自定义HTTP标头作为响应? [英] How to send custom HTTP header in response?
问题描述
我想在我的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'] ,
'allowed_hours'=> $ _ GET ['allowed_hours'],
'product'=>'mlc',
'prov_ur'=> NULL
);
header('Content-Type:application / json');
echo json_encode($ response);
但是当我打印我的标题我得到
< blockquote>
< div style =border:1px solid#990000; padding-left:20px; margin:0 0 10px 0;>
遇到PHP错误
严重性: p>
消息:未定义的索引:请求
文件名:admin / license.php
行号:22
$ b
遇到PHP错误
严重性:注意
消息:未定义的索引:
allowed_hours
文件名:admin / license.php
行号:
25
遇到PHP错误
严重性:注意
讯息:未定义的索引:
allowed_hours
文件名:admin / license.php
行号:
40
{response_code:200,device_id:70D0D01FBAD2,allowed_hours:null,product:mlc,prov_ur:null} {[Host] => string(14)192.168.50.123[Connection] =>
string(10)keep-alive[Cache-Control] => string (9)max-age = 0
[Accept] => string(74)
text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp, / ; q = 0.8
[Upgrade-Insecure-Requests] => string(1)1[User-Agent] =>
string(110)Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36
(KHTML,如Gecko)Chrome / 47.0.2526.111 Safari / 537.36
[Accept-Encoding] => string(19)gzip,deflate,sdch
[Accept-Language] => string(14)en-US,en; q = 0.8[Cookie] =>
string
cisession = OhhBBhVodwwf7Tb55AVsU32ClMS5cgmxBl15WHA%2BrGnvo1kiK%2B67BWeAuJVSV2MY25zZd0riHC9cyx9fiigiBuqkPMT%2FKE9d6et%2FXaE3F7I59P9%2FEzy5byQ5nEkJq5xwXoH1I7%2B7v62cQL21%2Bjfnk3AwIy4luM7N51IUuTqg7TxunoZFD1gJO84r8degY1imNpmDk2W%2FjsQPn9bQpkWJ9KVMxxViFDaELEU0rIfYmif%2BdvXjK9W%2Fj7iWQxZYE9ZGazgBTKlLO%2BJZHNdPrdmGPFTzTUROZdffpF%2Bb25bRMPEJsZ9CE2mdVuSn%2FEu678utd0lcd9bh%2BDbTDikrHP4jBFOLbZfWKT%2F9r5GkMBrLBl%2BlvPx9RbAq%2FIsjeA1V7c6JYf41TO1bG2XKT14QFHm8m0qY8HCal%2B%2BR8tZe9i3zy24%3Dcfc459942e4ef82a5554257216a19d621f446a25
[If-Modified-Since] => string(29)Thu,01 Jan 1970 00:00:00 GMT}
{response_code:200,device_id:70D0D01FBAD2,allowed_hours:null,product:mlc,prov_ur:null}
。我不想在我的HTTP标头回应中传送其他资料。
根据CI升级的代码
public function index()
{
$ request = array '=> $ this-> get('request'),
'device_id'=> $ this-> get('device_id'),
'launch_date'=> $ this - > get('launch_date'),
'allowed_hours'=> $ this-> get('allowed_hours'),
'site_id'=> $ this-& 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'),
'product'=>'mlc',
'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获取如下将有一些时间trow一些错误
$ _ GET ['allowed_hours'];
我建议更改为codeIgniter输入类方法。
$ this-> input-> get('allowed_hours');
Codeigniter 输入类会节省大量的代码。
使用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拥有输出类
和
$ 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));
i want to send json data in my HTTP Header.
I am using Codeigniter PHP. So i did this in my controller:
header('Content-Type: application/json');'
This is my code:
$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'],
'allowed_hours'=>$_GET['allowed_hours'],
'product'=>'mlc',
'prov_ur'=>NULL
);
header('Content-Type: application/json');
echo json_encode( $response );
But when i print my header i get
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
A PHP Error was encountered
Severity: Notice
Message: Undefined index: request
Filename: admin/license.php
Line Number: 22
A PHP Error was encounteredSeverity: Notice
Message: Undefined index: allowed_hours
Filename: admin/license.php
Line Number: 25
A PHP Error was encounteredSeverity: Notice
Message: Undefined index: allowed_hours
Filename: admin/license.php
Line Number: 40
{"response_code":200,"device_id":"70D0D01FBAD2","allowed_hours":null,"product":"mlc","prov_ur":null}array(10) { ["Host"]=> string(14) "192.168.50.123" ["Connection"]=>
string(10) "keep-alive" ["Cache-Control"]=> string(9) "max-age=0" ["Accept"]=> string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8" ["Upgrade-Insecure-Requests"]=> string(1) "1" ["User-Agent"]=>
string(110) "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"
["Accept-Encoding"]=> string(19) "gzip, deflate, sdch"
["Accept-Language"]=> string(14) "en-US,en;q=0.8" ["Cookie"]=>
string(518) "cisession=OhhBBhVodwwf7Tb55AVsU32ClMS5cgmxBl15WHA%2BrGnvo1kiK%2B67BWeAuJVSV2MY25zZd0riHC9cyx9fiigiBuqkPMT%2FKE9d6et%2FXaE3F7I59P9%2FEzy5byQ5nEkJq5xwXoH1I7%2B7v62cQL21%2Bjfnk3AwIy4luM7N51IUuTqg7TxunoZFD1gJO84r8degY1imNpmDk2W%2FjsQPn9bQpkWJ9KVMxxViFDaELEU0rIfYmif%2BdvXjK9W%2Fj7iWQxZYE9ZGazgBTKlLO%2BJZHNdPrdmGPFTzTUROZdffpF%2Bb25bRMPEJsZ9CE2mdVuSn%2FEu678utd0lcd9bh%2BDbTDikrHP4jBFOLbZfWKT%2F9r5GkMBrLBl%2BlvPx9RbAq%2FIsjeA1V7c6JYf41TO1bG2XKT14QFHm8m0qY8HCal%2B%2BR8tZe9i3zy24%3Dcfc459942e4ef82a5554257216a19d621f446a25" ["If-Modified-Since"]=> string(29) "Thu, 01 Jan 1970 00:00:00 GMT" }
{"response_code":200,"device_id":"70D0D01FBAD2","allowed_hours":null,"product":"mlc","prov_ur":null}
in my response. I dont want to send other data in my HTTP header response.
Upadted Code as per 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'),
'product'=>'mlc',
'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;
}
Using the normal php get like below will some times trow 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));
这篇关于如何发送自定义HTTP标头作为响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!