php的CURLOPT_USERPWD做了什么

Dom*_*nic 19 php curl basic-authentication

我想知道CURLOPT_USERPWD实际上对请求的url,标头或数据做了什么.它是INSTEAD OF Authorization: Basic <base64 of user:pass>还是它在这边工作?

是修改网址吗?:

username:password@someurl.com

我看到了一些像这样的代码,所以我想知道,因为看起来我在NodeJS等效请求中请求它的url只是一个Authorization标头(我有理论服务器坏了,忽略了Auth头并使用了用户名:密码在网址中):

    curl_setopt($ch, CURLOPT_URL, $url); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $encodedAuth = base64_encode(self::$pfAdapterUser.":".self::$pfAdapterPasswd);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authentication : Basic ".$encodedAuth));
    curl_setopt($ch, CURLOPT_USERPWD, self::$pfAdapterUser.":".self::$pfAdapterPasswd);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Run Code Online (Sandbox Code Playgroud)

谢谢

hls*_*lon 38

是修改网址吗?:

username:password@someurl.com

不,网址仍然相同.你可以查看

curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
Run Code Online (Sandbox Code Playgroud)

这个

$encodedAuth = base64_encode(self::$pfAdapterUser.":".self::$pfAdapterPasswd);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic ".$encodedAuth));
Run Code Online (Sandbox Code Playgroud)

还有这个

curl_setopt($ch, CURLOPT_USERPWD, self::$pfAdapterUser.":".self::$pfAdapterPasswd);
Run Code Online (Sandbox Code Playgroud)

正在做同样的事情所以没有必要一起使用它们(尽管它不会破坏),使用一个它会工作正常.

  • @OliverDixon它将用户名和密码转换为其base64形式并在前面添加“Authorization: Basic”,所以基本上它会将其发送为“Authorization: Basic &lt;base64 form of username:password&gt;” (2认同)