How do I make a request using HTTP basic authentication with PHP curl?

How do I make a request using HTTP basic authentication with PHP curl?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

HTTP Basic Authentication in PHP Curl: A Practical Guide

When building a REST web service client in PHP, you will inevitably need to handle authentication. If your target API requires HTTP Basic Authentication, using the curl extension is the standard approach. The core question often arises: do you have to manually calculate the Base64 hash and inject the headers? The short answer is no. While manual header injection works, leveraging built-in curl options reduces the risk of implementation errors and keeps your code cleaner.

HTTP Basic Authentication relies on a specific handshake where the client sends credentials in an Authorization header. The value of this header must be the prefix "Basic " followed by a Base64-encoded string of the username and password concatenated with a colon (e.g., username:password). Manually constructing this string is error-prone; you might accidentally encode just the username, forget the colon, or mishandle special characters. The curl library abstracts this complexity away, allowing you to pass credentials directly as arguments.

The most straightforward method involves using the CURLOPT_USERPWD option combined with CURLOPT_HTTPAUTH. When you set these options, curl automatically generates the correct Base64 string and sets the header for you. Consider the following implementation pattern:

$url = 'https://api.example.com/v1/resource';
$username = 'my_user';
$password = 'secret_pass';

$ch = curl_init($url);

// Define the username and password
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);

// Enable HTTP Basic Authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

// Execute the request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpCode === 200) {
    // Handle success
} else {
    // Handle authentication failure or other errors
}

curl_close($ch);

This approach is robust because it separates the logic of "who we are" from the mechanics of "how we talk." You specify the identity once, and curl handles the protocol details. However, there are trade-offs to consider regarding security and flexibility.

One significant pitfall with CURLOPT_USERPWD is handling credentials in memory. Since you are passing a string containing the password directly into the options array, sensitive data resides in the variable scope until the request completes. For high-security applications, this might require additional precautions, such as clearing variables immediately after execution or utilizing environment variables to source the password rather than hardcoding it.

Additionally, you must be aware of curl's default behavior regarding redirects. If your API requires authentication on a redirect (a 301 or 302 response), curl will automatically follow the redirect and re-attach the credentials to the new URL. This is usually desirable, but in rare cases where an intermediary service needs to inspect the original request without following the redirect chain, you would need to adjust CURLOPT_FOLLOWLOCATION.

If your project is built on top of a framework like Laravel, managing raw curl instances can sometimes feel disconnected from the rest of the application. In those scenarios, utilizing the HTTP client provided by the framework might offer better integration with service containers and middleware pipelines. For instance, if you are maintaining a legacy codebase or need fine-grained control over specific low-level options that aren't exposed by a helper class, sticking with curl is perfectly valid.

Ultimately, the decision to use manual header construction versus curl's built-in options usually comes down to maintainability. The overhead of manually calculating the Base64 hash and setting the Authorization: Basic ... header yourself is rarely worth the marginal gain in control for standard REST clients. Letting the library handle the encoding ensures you stay compliant with RFC 7617 without introducing subtle bugs into your authentication logic.

Our network