How to use basic authorization in PHP curl
When migrating from command-line tools like curl to server-side scripting, a common stumbling block is handling authentication headers correctly. You might have a working command line request that looks something like this:
curl -H "Accept: application/product+xml" "https://{id}:{api_key}@api.domain.com/products?limit=1&offset=0"
In this scenario, the URL includes the credentials directly in the host portion of the string. While convenient for testing from your terminal, replicating this behavior in PHP requires understanding exactly how curl processes that URL versus how it handles explicit headers.
The core issue arises when developers attempt to manually set the Authorization: Basic ... header. You might try passing the raw ID and API key separated by a colon, or wrapping them in curly braces as shown in your command line example. The latter syntaxâ{id}:{api_key}âis almost certainly incorrect for the HTTP Authorization header itself. The curl CLI tool parses the URL string, extracts the credentials from the host segment, encodes them, and then sends a request with a clean Authorization: Basic <base64-encoded-credentials> header. It does not forward the literal curly braces or the host-based syntax into the header value.
To replicate this in PHP, you have two distinct approaches depending on whether your API documentation supports credentials in the URL or expects them strictly in the header. If the API is strict and requires the Authorization header, you must encode the credentials yourself. The standard for Basic Auth is Base64 encoding of the username:password string (or id:api_key in your case).
Here is how to construct the request correctly using the PHP curl extension:
$url = 'https://api.domain.com/products?limit=1&offset=0';
$auth = base64_encode($id . ':' . $api_key);
$ch = curl_init();
// Set the headers manually
$headers = [
'Accept: application/product+xml',
'Authorization: Basic ' . $auth
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
// Handle error
echo 'Error: ' . curl_error($ch);
} else {
// Process response
echo $response;
}
curl_close($ch);
Notice that the URL passed to CURLOPT_URL does not contain the credentials. The header value is purely Basic <base64-string>. If you see an error like "authentication parameter in the request are missing or invalid," it usually means the server is rejecting the manually constructed header because the format doesn't match what the API expects, or you are inadvertently sending malformed data.
However, many modern APIs actually prefer or require credentials to be passed directly in the URL host portion (https://{id}:{api_key}@host) rather than via the Authorization header. If your API documentation specifies Basic Auth but implies URL-based authentication (as seen in your original command), you should try setting the URL directly in PHP without manually adding the Authorization header at all.
$url = "https://{$id}:{$api_key}@api.domain.com/products?limit=1&offset=0";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/product+xml'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
The trade-off here is security and maintainability. Passing credentials in the URL exposes them to log files if logging is enabled on your server or load balancer. If you are building a long-term solution, especially within a framework like Laravel, it is generally better practice to use the Authorization header with proper Base64 encoding. This keeps secrets out of the request URI and allows for easier token rotation or switching between Basic Auth and OAuth later without changing the endpoint URL.
If you are working in a Laravel environment, you can leverage the HTTP client facade which handles these details more cleanly:
$response = Http::withHeaders([
'Accept' => 'application/product+xml',
'Authorization' => 'Basic ' . base64_encode($id . ':' . $api_key),
])->get("https://api.domain.com/products?limit=1&offset=0");
Understanding the distinction between URL-based authentication and header-based authentication is key. The command line tool abstracts this complexity away, but server-side code demands explicit handling of encoding and headers to ensure compatibility with the API's expected input format.
