如何禁用php中的CURLOPT_HTTPAUTH选项? [英] How do I disable the CURLOPT_HTTPAUTH option in php?

查看:939
本文介绍了如何禁用php中的CURLOPT_HTTPAUTH选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我到目前为止:

  self :: $ connection = curl_init 
curl_setopt(self :: $ connection,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt(self :: $ connection,CURLOPT_USERPWD,$ username。:。
curl_setopt(self :: $ connection,CURLOPT_URL,$ url);

curl_exec(self :: $ connection); //做一个使用Basic Auth的请求
curl_setopt(self :: $ connection,CURLOPT_HTTPAUTH,false); //< - 不按预期工作 - 我想禁用基本Auth
curl_setopt(self :: $ connection,CURLOPT_URL,$ anotherURL);
curl_exec(self :: $ connection); //< - - 不按预期工作 - 我想做一个不使用基本Auth的请求。因此,如果我将CURLOPT_HTTPAUTH选项初始化为CURLAUTH_BASIC,我将如何禁用它? p>

我需要使用相同的句柄(即self :: $ connection)才能拥有一个持久的HTTP连接。

解决方案

如果它帮助任何人,这是我最后做的:

  if($ enableBasicAuth){
self :: $ httpHeaders ['Authorization'] ='Basic'.base64_encode($ username:$ password);
}
else if(isset(self :: $ httpHeaders ['Authorization'])){
unset(self :: $ httpHeaders ['Authorization']); //禁用基本认证
}

//将$ httpHeaders数组转换为CURLOPT_HTTPHEADER使用的格式
$ httpHeadersRaw = array();
foreach(self :: $ httpHeaders as $ header => $ value){
$ httpHeadersRaw [] = $ header。':'。
}
curl_setopt(self :: $ connection,CURLOPT_HTTPHEADER,$ httpHeadersRaw); //手动设置HTTP基本认证头

基本上我只是手动启用/禁用基本认证使用CURLOPT_HTTPHEADER选项。


So this is what I have so far:

self::$connection = curl_init();
curl_setopt(self::$connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(self::$connection, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt(self::$connection, CURLOPT_URL, $url);

curl_exec(self::$connection); // Do a request that uses Basic Auth
curl_setopt(self::$connection, CURLOPT_HTTPAUTH, false); // <-- Not working as expected - I want to disable Basic Auth here
curl_setopt(self::$connection, CURLOPT_URL, $anotherURL);
curl_exec(self::$connection); // <-- Not working as expected - I want to do a request that does NOT use Basic Auth.

So, if I initialized the CURLOPT_HTTPAUTH option to CURLAUTH_BASIC, how would I go about disabling it?

I need to use the same handle (that is self::$connection) in order to have a persistent HTTP connection.

解决方案

If it helps anyone, this is what I ended up doing:

if ($enableBasicAuth){
    self::$httpHeaders['Authorization'] = 'Basic '.base64_encode("$username:$password");    
}
else if (isset(self::$httpHeaders['Authorization'])){
    unset(self::$httpHeaders['Authorization']);    // Disable Basic Auth
}

// Convert the $httpHeaders array into a format that is used by CURLOPT_HTTPHEADER
$httpHeadersRaw = array();
foreach (self::$httpHeaders as $header=>$value){
    $httpHeadersRaw[] = $header.': '.$value;
}
curl_setopt(self::$connection, CURLOPT_HTTPHEADER, $httpHeadersRaw); // Set the HTTP Basic Auth header manually

Basically I just manually enable/disable Basic Auth using the CURLOPT_HTTPHEADER option.

这篇关于如何禁用php中的CURLOPT_HTTPAUTH选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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