HTTP Basic Auth with HTTP headers and libcurl

[problem]

Came across an interesting problem with curl.

For curl to perform HTTP Basic Authentication, it is easy to pass –user to the curl command, but harder with libcurl.

Suspect there is an attribute that can be set, but I monitor a multitude of web sites through some perl scripts and libcurl. I did n’t want to have to modify my wrapper scripts, which mesh perl hashes with the code that drives curl (via libcurl).

I do allow for headers though, having needed to pass different things through, like HTTP_REFERER, LAST_MODIFIED, etc.

Therefore I just needed to pass the HTTP BASIC Authentication through as a header.

[/problem]

[solution]

First off you need to base64 encode the user and password.

Use Google’s tool

[/solution]

[example]

We then strip out the equals and pass following through to curl or libcurl:

 @myheaders=('Authorization: Basic YWRtaW46YWRtaW4');  Curl::easy::setopt($curl, Curl::easy::CURLOPT_HTTPHEADER, @myheaders); 

Or from the command line:

 curl ... -H'Authorization: Basic YWRtaW46YWRtaW4' 

Obviously you need to put your user, followed by a colon and your password – to obtain the correct base64 encoding back – strip the equals out and away you go.

[/example]