Setting HTTP Headers via CURL in PHP
Using the option CURLOPT_HTTPHEADER
you can send HTTP request headers when making a request.
This option accepts an array of headers and their values
as a parameter.
Let's set this option by passing some headers:
<?php
$headers = [
'Accept-Language: en-US',
'Accept: text/html'
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
?>
On the page to which we are sending the request, you can check that the specified headers were sent:
<?php
var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);
var_dump($_SERVER['HTTP_ACCEPT']);
?>
Send a request to the following
page, specifying the X-Test header:
<?php
echo $_SERVER['HTTP_X_TEST'];
?>
Send a request to the following page and get the result:
<?php
if ($_SERVER['HTTP_X_TEST'] === '12345') {
echo 'result';
} else {
echo 'error';
}
?>