The header() function is completely ignored by the SAPI CLI. However, this affects SAPI in Apache and CGI.
Simply put, the CLI SAPI does not implement any logic in the sapi_*_header_* functions. For example, for the CLI SAPI, in php_cli.c :
static int sapi_cli_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) { return SAPI_HEADER_SENT_SUCCESSFULLY; }
All of these functions basically return NULL , 0 or a false success message.
For CGI SAPI in cgi_main.c :
static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) { char buf[SAPI_CGI_MAX_HEADER_LENGTH]; sapi_header_struct *h; zend_llist_position pos; zend_bool ignore_status = 0; int response_status = SG(sapi_headers).http_response_code;
You can easily do this work using binary, etc. php-cgi arrays:
server.php
$script_to_run = 'script.php'; exec('php-cgi '.escapeshellarg($script_to_run), $output); $separator = array_search("", $output); $headers = array_slice($output, 0, $separator); $body = implode("\r\n", array_slice($output, $separator+1)); print_r($headers); print $body;
script.php
header('Content-Type: text/plain'); echo 'Hello, World!';
Output:
Array ( [0] => X-Powered-By: PHP/5.3.8 [1] => Content-Type: text/plain ) Hello, World!
netcoder
source share