在标题 php curl 中发送身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13187462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending auth in headers php curl
提问by ven
trying to do the equivalent of this in PHP - and failing :):
尝试在 PHP 中执行与此等效的操作 - 但失败了 :):
curl -H "X-abc-AUTH: 123456789" http://APIserviceProvider=http://www.cnn.com;
"123456789" is the API key. The command line statement works fine.
“123456789”是 API 密钥。命令行语句工作正常。
PHP code (does not work):
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);
Any ideas?
有任何想法吗?
回答by sergiocruz
In order to get custom headers into your curl you should do something like the following:
为了将自定义标题放入 curl,您应该执行以下操作:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Some_custom_header: 0',
'Another_custom_header: 143444,12'
));
Therefore the following should work in your case (given X-abc-AUTH is the only header you need to send over):
因此,以下应该适用于您的情况(鉴于 X-abc-AUTH 是您需要发送的唯一标头):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-abc-AUTH: 123456789' // you can replace this with your $auth variable
));
If you need additional custom headers, all you have to do is add on to the array within the curl_setopt.
如果您需要额外的自定义标头,您所要做的就是添加到 curl_setopt 中的数组。
I hope this helps :)
我希望这有帮助 :)
回答by Dadaso Zanzane
Use the following Syntax
使用以下语法
$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 ;
回答by deb0rian
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth
));
回答by hakre
You set only one request header, not the two you wanted. You could do it for example like this:
您只设置了一个请求标头,而不是您想要的两个。例如,您可以这样做:
// 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;
(change the service url to the right one to get this to work)
(将服务网址更改为正确的网址以使其正常工作)

