如何使用php向HTTP响应头添加值

时间:2015-05-07 03:38:58

标签: php curl

到目前为止,我设法从此处获得HTTP响应标题

$ch = curl_init();<br/>
$url="http://localhost/PHP_Projects/Test/response.php";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

$headers = get_headers_from_curl_response($response);

foreach($headers as $x => $x_value){<br/>
    print $x.": ".$x_value;<br/>
}

function get_headers_from_curl_response($response)
{<br/>
    $headers = array();

    $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));

    foreach (explode("\r\n", $header_text) as $i => $line)
        if ($i === 0)
            $headers['http_code'] = $line;
        else
        {
            list ($key, $value) = explode(': ', $line);

            $headers[$key] = $value;
        }

    return $headers;
}



这样的输出就像是

HTTP/1.1 200 OK 
Date: Thu, 07 May 2015 03:26:26 GMT
Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
X-Powered-By: PHP/5.6.8
Content-Length: 128
Content-Type: text/html; charset=UTF-8

但我想在此添加更多内容,如

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Path=/application_uri
Freeflow: FC
charge: Y
amount: 100
Expires: -1
Pragma: no-cache
Cache-Control: max-age=0
Content-Type: UTF-8
Content-Length: 20

1 个答案:

答案 0 :(得分:2)

$headers = array( 'Path: application_uri',
                  'Freeflow: FC',
                  'charge: Y',
                  'amount: 100',
                 );
curl_setopt ($ch, CURLOPT_HTTPHEADER,$headers);