Get and Set custom http headers in php
suggest changeSending The Request Header
$uri = 'http://localhost/http.php';
$ch = curl_init($uri);
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array('X-User: admin', 'X-Authorization: 123456'),
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_VERBOSE => 1
));
$out = curl_exec($ch);
curl_close($ch);
// echo response output
echo $out;
Reading the custom header
print_r(apache_request_headers());
Example of apache_request_headers():
The Content-Length and Content-Type lines below come from a request that sent a body. The curl example above does not.
Array
(
[Host] => localhost
[Accept] => */*
[X-User] => admin
[X-Authorization] => 123456
[Content-Length] => 9
[Content-Type] => application/x-www-form-urlencoded
)
The same kind of header can be sent from the shell:
curl --header "X-MyHeader: 123" www.google.com
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents