Paul Dessert Paul Dessert - 28 days ago 12
PHP Question

Sending username and password with cURL

I'm trying to access an API using php cURL. The docs say:


Each request must include the API Key that was assigned to the email
partner. It should be

presented to the server via standard Basic HTTP authentication (as
defined in RFC2617) as

the username with an empty password.


How do I send the API key as the username and leave the PW empty? Here is what I have so far:

$subscriberInfo = [
'email' => $email,
'search' => $jobType,
'location' => $location
];

$ch = curl_init('https://api.ExternalSiteHere');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $subscriberInfo);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data'
));

// execute!
$response = curl_exec($ch);

// close the connection
curl_close($ch);

var_dump($response);
die();

Answer

curl_setopt($ch, CURLOPT_USERNAME, base64_encode("{$username}:{$password}"));

In your case

curl_setopt($ch, CURLOPT_USERNAME, base64_encode("{$username}:"));

If that doesn't work drop the colon after username

Comments