Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL Basic Authentication Alternatives for CURLOPT_HTTPHEADER & CURLOPT_USERPWD...?

Are there any alternatives to using CURLOPT_HTTPHEADER & CURLOPT_USERPWD for supplying Basic Authentication for cURL PHP?

I have a super long password, so CURLOPT_USERPWD wont work as it truncates at 256 characters.

curl_setopt($data, CURLOPT_USERPWD, $username . ":" . $password);

And I would like to stay away from using CURLOPT_HTTPHEADER for security reasons.

curl_setopt($data, CURLOPT_HTTPHEADER, "Authorization: Basic " . base64_encode($username . ":" . $password));

Any alternatives?

like image 754
Jay S. Avatar asked Nov 30 '12 23:11

Jay S.


People also ask

How to use basic auth in PHP cURL?

To send basic auth credentials with Curl, use the "-u login: password" command-line option. Curl automatically converts the login: password pair into a Base64-encoded string and adds the "Authorization: Basic [token]" header to the request.

How to pass authorization Bearer Token in cURL PHP?

Sending the Bearer Token with a Curl POST request is similar to sending the Bearer Token with a Curl GET request. POST data is passed with the -d command-line option, and the authorization header and the bearer token are passed with the -H command-line option.

What is CURL_ setopt in PHP?

curl_setopt — Set an option for a cURL transfer.

What is Curl_exec?

curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.


1 Answers

What makes you think CURLOPT_HTTPHEADER is disabled for security reasons?

It accepts an array rather than a string. Try this instead:

curl_setopt($data, CURLOPT_HTTPHEADER,
            array(
              "Authorization: Basic " . base64_encode($username . ":" . $password)
));
like image 111
drew010 Avatar answered Sep 22 '22 13:09

drew010