【问题标题】:Sending auth in headers php curl在标头php curl中发送身份验证
【发布时间】:2012-10-22 15:41:04
【问题描述】:

尝试在 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);

有什么想法吗?

【问题讨论】:

  • 如果您的命令行示例有 X-abc-AUTH: 标头,为什么还要使用 Content-Type
  • 我收到“需要内容类型”错误。但我只是想通了!我已经更新了上面的代码。

标签: php authentication curl header


【解决方案1】:

为了将自定义标题添加到您的 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 内的数组中。

我希望这会有所帮助:)

【讨论】:

  • 我正在努力解决服务器只接收第一个数组项的问题。也许有人可以帮助解决这个stackoverflow.com/questions/49771245/…
  • 我将它用于 AWS AppSync GraphQL 的 PHP/curl 身份验证标头。谢谢小伙伴们的帮助。 'x-api-key: '.$authToken
【解决方案2】:

使用以下语法

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

【讨论】:

    【解决方案3】:
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'auth=' . $auth
    ));
    

    【讨论】:

    • 那么为什么上面的同一篇文章有​​ + 而我有 - ? :)
    • 这几乎成功了!这使它工作: curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth));
    • 嗯,是的,我只是提出了如何发送自定义 http 标头的想法,并不是要 100% 工作的解决方案,只是语法。不知道为什么我有缺点:(
    【解决方案4】:

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

    // 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;
    

    (将服务网址更改为正确的网址以使其正常工作)

    【讨论】:

    • 感谢您的建议 - 似乎它会起作用,但它没有:(我也更改了服务 url + 密钥......再次感谢。
    • 你可能想在curl_exec:var_dump(curl_error($ch));之后做一些错误检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2019-02-21
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    相关资源
    最近更新 更多