php 的 CURLOPT_USERPWD 有什么作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23809392/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:58:24  来源:igfitidea点击:

What does php's CURLOPT_USERPWD do

phpcurlbasic-authentication

提问by Dominic

I was wondering what CURLOPT_USERPWDis actually doing to the url, header or data of a request. Is it INSTEAD OF the Authorization: Basic <base64 of user:pass>or does it work along side this?

我想知道CURLOPT_USERPWD实际上对请求的 url、标题或数据做了什么。它是代替的Authorization: Basic <base64 of user:pass>还是与此一起工作?

Is it modifying the url to this?:

是否将 url 修改为此?:

username:[email protected]

username:[email protected]

I saw some code like this so I am wondering, as it seems if I request that url in a NodeJS equivalent request it is not working with just an Authorization header (I have a theory the server is broken and ignoring the Auth header and using the username:password in the url):

我看到了一些这样的代码,所以我想知道,因为似乎如果我在 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);

Thanks

谢谢

回答by hlscalon

Is it modifying the url to this?:

username:[email protected]

是否将 url 修改为此?:

username:[email protected]

No, the url still the same. You can check with

不,网址还是一样。你可以检查

curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

This

这个

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

And this

和这个

curl_setopt($ch, CURLOPT_USERPWD, self::$pfAdapterUser.":".self::$pfAdapterPasswd);

are doing the same thing so there's no need to use them together (although it won't break), use one and it will work fine.

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