Base64 Encode username and password in PHP

marcomarquez February 20, 2013

Hi Guys,

I'm having trouble getting issues from my account when doing Base 64 encoding for Authorization, is there a way to do this with curl within PHP?

Here's my code:

<?php
 
$username = 'testclient';
$password = 'test@123';
  
$url = "https://xxxxxx.atlassian.net/rest/api/2/project";
$ch = curl_init();
  
$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);
  
$test = "This is the content of the custom field.";
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  
$result = curl_exec($ch);
$ch_error = curl_error($ch);
  
if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    echo $result;
}
  
curl_close($ch);
 
?>

If I leave the code as is, it works. However, if I change my username and password to a Base64 string and replace it with something like:

$base64string = 'asdlfkjadslfjasdflkdasf'

...

curl_setopt($ch, CURLOPT_USERPWD, $base64string);

it doenst work :(

What am I doing incorrectly?

Thanks!

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
OmarA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 23, 2013

You can try to set the headers manually, see : http://www.php.net/manual/en/function.curl-setopt.php#80271

TAGS
AUG Leaders

Atlassian Community Events