在标题php curl中发送auth

时间:2012-11-02 00:15:51

标签: php authentication curl header

尝试在PHP中执行相当于此操作 - 并且失败:):

curl -H "X-abc-AUTH: 123456789" http://APIserviceProvider=http://www.cnn.com;

“123456789”是API密钥。命令行语句工作正常。

PHP代码(不起作用):

$urlToGet = "http://www.cnn.com";
$service_url = "http://APIserviceProvider=$urlToGet";

//header

 $contentType = 'text/xml';          //probably not needed
 $method = 'POST';                   //probably not needed
 $auth = 'X-abc-AUTH: 123456789';    //API Key

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

//does not work



// curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: ' . 
   // $contentType . '; auth=' . $auth));

    //works!   (THANKS @Fratyr for the clue):

    curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth));

//this works too (THANKS @sergiocruz):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Some_custom_header: 0',
  'Another_custom_header: 143444,12'
));


//exec

$data = curl_exec($ch);
echo $data;
curl_close($ch);

有什么想法吗?

4 个答案:

答案 0 :(得分:17)

为了将自定义标题添加到您的curl中,您应该执行以下操作:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Some_custom_header: 0',
  'Another_custom_header: 143444,12'
));

因此,以下情况适用于您的情况(假设X-abc-AUTH是您需要发送的唯一标头):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'X-abc-AUTH: 123456789' // you can replace this with your $auth variable
));

如果您需要其他自定义标题,您只需在curl_setopt中添加数组。

我希望这会有所帮助:)

答案 1 :(得分:3)

使用以下语法

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = array();
$headers[] = 'X-abc-AUTH: 123456789';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$headers[] = 'Host: 202.71.152.126';
$headers[] = 'Referer: http://www.example.com/index.php'; //Your referrer address
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0';
$headers[] = 'X-MicrosoftAjax: Delta=true';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;

答案 2 :(得分:0)

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth
));

答案 3 :(得分:0)

您只设置了一个请求标头,而不是您想要的两个请求标头。你可以这样做:

// input
$urlToGet    = "http://www.cnn.com";

// url
$service_url = sprintf("http://APIserviceProvider=%s", urlencode($urlToGet));

//header
$contentType = 'Content-type: text/xml'; //probably not needed
$auth        = 'X-abc-AUTH: 123456789'; //API Key
$method      = 'POST'; //probably not needed

// curl init
$ch = curl_init($service_url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLINFO_HEADER_OUT    => true,
    CURLOPT_HTTPHEADER     => [
        $contentType,
        $auth,
    ],
]);

// curl exec
$data = curl_exec($ch);
curl_close($ch);

// output
echo $data;

(将服务网址更改为正确的服务网址)