【问题标题】:How to send custom HTTP header in response?如何发送自定义 HTTP 标头作为响应?
【发布时间】:2016-04-29 21:47:57
【问题描述】:

我想在我的 HTTP Header 中发送 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 );

但是当我打印我的标题时,我得到了

遇到 PHP 错误

严重性:通知

消息:未定义索引:请求

文件名:admin/license.php

行号:22

遇到 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}array(10) { ["主机"]=> 字符串(14) "192.168.50.123" ["连接"]=>
字符串(10)“保持活动”[“缓存控制”]=>字符串(9)“最大年龄=0” [“接受”]=> 字符串(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8" ["Upgrade-Insecure-Requests"]=> string(1) "1" ["User-Agent"]=>
字符串(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"]=>
字符串(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}

在我的回复中。我不想在我的 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'),
        '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;

}

【问题讨论】:

  • 错误解释了问题 - 缺少 GET 变量 - 确保在发送响应之前设置它们
  • @RamRaider 我已经在数组 $request 中设置了我的 GET 变量
  • Message: Undefined index: allowed_hours这行提示需要先设置:'allowed_hours'
  • @AddWebSolutionPvtLtd 感谢您的关注
  • 谢谢@RamRaider 我明白了

标签: php codeigniter http-headers phpwebsocket


【解决方案1】:

像下面这样使用普通的 php get 有时会抛出一些错误

$_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'),
    '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));

【讨论】:

  • 我正在更新我的代码,请检查我没有得到正确的响应
  • 你得到了 set_output();
  • 在我的代码之后添加了这个:echo $this->output->set_output(json_encode($response));
  • 我应该会在屏幕上回显 JSON 数据
  • 你的 $this->get() 错了。应该是 $this->input->get() 现在能用了吗
【解决方案2】:

问题是您的某些 $_GET 变量未设置,这将引发错误(您拥有的额外输出文本)并且可以通过在使用它们之前检查它们是否为空来防止。

$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 );

【讨论】:

  • Codeigniter 输入类方法更简单,代码更少。
  • @wolfgang1983 我知道,我从来没有说过这是最好的解决方案。这只是众多可能的解决方案之一;)
猜你喜欢
  • 2016-05-27
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多