如何在php中添加自定义请求标头

i want to send a custom header to a domain.

i tried like the following :

header("myheader: value1");
//i want to redirect above header to a samplesite now
header('Location: http://localhost/samplesite', FALSE);
exit;

And now in samplesite, I could not get myheader. How to achieve it, please help me.

You can simply send a custom header with file_get_contents() and give it a context.

It would look something like this:

$data = http_build_query(array("username" => $uid));
$opts = array (
    'http' => array (
        'method' => 'POST',
        'header'=> "Content-type: application/x-www-form-urlencoded
"
            . "Content-Length: " . strlen($data) . "
",
        'content' => $data
        )
    );

$context  = stream_context_create($opts);
$returnedData= file_get_contents("example.com", false, $context);

Remember that the remote host needs to allow this kind of requests or you will get an error

The header() function you tried to use in your example will just change the header send back from your server so header('Location: http://localhost/samplesite', FALSE); would just be a simple redirect to that site.