在头PHP卷曲发送身份验证 [英] Sending auth in headers php curl

查看:104
本文介绍了在头PHP卷曲发送身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

努力做这相当于在PHP - 和失败:):

trying to do the equivalent of this in PHP - and failing :):

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

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

"123456789" is the API key. The command line statement works fine.

PHP code(不工作):

PHP code (does not work):

$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);

任何想法?

推荐答案

为了让自定义页眉到卷曲你应该做类似如下:

In order to get custom headers into your curl you should do something like the following:

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

因此​​,以下应该在你的情况下工作(给定的X-ABC-AUTH是你需要寄过来的只有头):

Therefore the following should work in your case (given X-abc-AUTH is the only header you need to send over):

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

如果您需要更多的自定义标题,你所要做的就是在添加到curl_setopt内的数组。

If you need additional custom headers, all you have to do is add on to the array within the curl_setopt.

我希望这有助于:)

这篇关于在头PHP卷曲发送身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆