2015-09-21 161 views
0

我想将自定义标头发送到域。如何在php中添加自定义请求标头

我试着像下面这样:

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

现在在samplesite,我不能让myheader。 如何实现它,请帮助我。

回答

0

您可以简单地使用file_get_contents()发送自定义标题并为其指定上下文。

这将是这个样子:

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

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

记住远程主机需要允许这种请求的,否则你会得到一个错误

头()函数,你试图使用你的例子只会改变从你的服务器发回的头,所以header('Location: http://localhost/samplesite', FALSE);只是一个简单的重定向到该网站。

相关问题