Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does php's CURLOPT_USERPWD do

I was wondering what CURLOPT_USERPWD is 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?

Is it modifying the url to this?:

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):

    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

like image 599
Dominic Avatar asked May 22 '14 14:05

Dominic


People also ask

What is Curlopt_userpwd?

Name. CURLOPT_USERPWD - user name and password to use in authentication.

What does Curl_exec return?

Returns true on success or false on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is Curlopt_encoding?

CURLOPT_ENCODING only tells the server what types of encoding it will accept. So either way the data will only be decoded if needed. But by giving CURLOPT_ENCODING a value you are limiting your call to only accept one type of encoding.

What is curl_init?

The curl_init() function will initialize a new session and return a cURL handle. curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).


1 Answers

Is it modifying the url to this?:

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.

like image 166
hlscalon Avatar answered Sep 23 '22 07:09

hlscalon