如何将自定义标头添加到https请求curl PHP脚本?

时间:2014-07-22 07:28:19

标签: php curl

我正在编写以下代码

<?php
$ch=curl_init(); 
$h=array("Host:v.airtellive.com");
curl_setopt($ch,CURLOPT_URL,"https://facebook.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$h);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow header redirections
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to 4
$p=curl_getinfo($ch);
$o=curl_exec($ch);
if($o==null)
var_dump (curl_error($ch)); 
$headers=curl_getinfo($ch,CURLINFO_HEADER_OUT);
print_r($headers);
echo "<xmp>";
echo $o;

curl_close($ch);
?>

但它给了我错误:

  

无法解析主机

如果我将此脚本与search.yahoo.com一起使用,则可以正常使用。实际上,我想通过发送主机头来创建realhost代理。

1 个答案:

答案 0 :(得分:0)

CURL的标题必须像这样设置

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER , array(
                "AUTH_USERNAME: {$username}",
                "AUTH_PASSWORD: {$password}",
            ));
$curlReturn = curl_exec($curl_handle);
    curl_close($curl_handle);

任何数量的自定义标头都可以作为数组发送。