如何发送自定义 HTTP 标头作为响应?

Raj*_*jan 5 php codeigniter http-headers phpwebsocket

我想在我的 HTTP 标头中发送 json 数据。

我使用的是 Codeigniter PHP,所以我在我的控制器中做了这个:

header('Content-Type: application/json');'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

$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 );
Run Code Online (Sandbox Code Playgroud)

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

遇到 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) { ["Host"]=> string(14) ) "192.168.50.123" ["Connection"]=>
string(10) "keep-alive" ["Cache-Control"]=> string(9) "max-age=0" ["Accept"]=> string (74) “text / html的,应用/ XHTML + xml的,应用/ XML; q = 0.9,图像/ WEBP,/ q = 0.8”[ “升级不安全-请求”] =>串(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"]=>
串(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}
Run Code Online (Sandbox Code Playgroud)

在我的回应中。我不想在我的 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;

}
Run Code Online (Sandbox Code Playgroud)

小智 1

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

\n\n
$_GET[\'allowed_hours\'];\n
Run Code Online (Sandbox Code Playgroud)\n\n

我建议更改 codeIgniter 输入类方法。

\n\n
$this->input->get(\'allowed_hours\');\n
Run Code Online (Sandbox Code Playgroud)\n\n

Codeigniter输入类将为您节省大量代码。

\n\n

正如用户指南中所述:使用 CodeIgniter\xe2\x80\x99s 内置方法,您可以简单地执行以下操作:

\n\n

更新:

\n\n
$request = array(\n   \'request\' => $this->input->get(\'request\'),\n   \'device_id\' => $this->input->get(\'device_id\'),\n   \'launch_date\'=> $this->input->get(\'launch_date\'),\n   \'allowed_hours\'=> $this->input->get(\'allowed_hours\'),\n   \'site_id\'=> $this->input->get(\'site_id\'),\n   \'product\'=>$this->input->get(\'product\'),\n   \'software_version\'=> $this->input->get(\'software_version\'),\n   \'platform_os\'=> $this->input->get(\'platform_os\'),\n   \'platform\'=> $this->input->get(\'platform\'),\n   \'platform_model\'=> $this->input->get(\'platform_model\')\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

Codeigniter 有自己的输出类

\n\n

\n\n
$response = array(\n    \'response_code\' => 200,\n    \'device_id\'=> $this->input->get(\'device_id\'),\n    \'allowed_hours\'=> $this->input->get(\'allowed_hours\'),\n    \'product\'=> \'mlc\',\n    \'prov_ur\'=> NULL \n);\n\nreturn $this->output\n      ->set_content_type(\'Content-Type: application/json\')\n      ->set_output(json_encode($response));\n
Run Code Online (Sandbox Code Playgroud)\n