Making a HTTP GET request with HTTP-Basic authentication

Making a HTTP GET request with HTTP-Basic authentication

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

Stefan Bogdanescu

Founder & Senior Architect

Making a HTTP GET request with HTTP-Basic authentication in PHP

Building a proxy for legacy Flash Player projects often requires bridging modern web infrastructure with older client expectations. When the requirement is strictly to forward an HTTP GET request to another URL using HTTP-Basic authentication and serve the raw response back to the client, you are essentially implementing a lightweight reverse proxy. The core challenge lies not in fetching the data, but in correctly managing the authentication headers and ensuring the response stream is passed through without interference from PHP's default error reporting or output buffering.

The most direct approach involves using cURL, which provides fine-grained control over request methods, headers, and body content. You must explicitly enable the transfer of the response body and ensure that no additional data is appended to it. A common pitfall in this scenario is forgetting to disable SSL verification warnings or ignoring the need to forward specific headers from the upstream server, though for basic authentication, the focus remains on the Authorization header.

Here is a functional example using cURL within a PHP context. This snippet initiates a GET request to a target URL, applies Basic auth credentials, and writes the response directly to the output buffer:

$url = 'https://api.example.com/data';
$username = 'myuser';
$password = 'mypassword';

$ch = curl_init($url);

// Enable following redirects if the upstream server uses them
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // Important: Output directly to stdout

// Set Basic Authentication
$userPWD = base64_encode($username . ':' . $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $userPWD);

// Ensure SSL verification is strict to prevent man-in-the-middle attacks
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

// Execute the request
$result = curl_exec($ch);

if ($result === false) {
    // Handle errors appropriately before outputting to the client
    echo 'Proxy Error: ' . curl_error($ch);
} else {
    // Clear any potential PHP buffering that might have captured headers
    ob_clean();
    echo $result;
    
    // Return the original HTTP status code if available via cURL info
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    http_response_code($status);
}

curl_close($ch);

In this implementation, setting CURLOPT_RETURNTRANSFER to false is critical. If set to true, cURL would store the response in a string variable rather than sending it directly to the browser. Since your goal is to serve the response as if you were the original source, you want the data to stream immediately. Furthermore, clearing the output buffer with ob_clean() ensures that any headers generated by previous PHP statements (like debugging logs) do not pollute the response stream, which could cause Flash Player or other strict clients to fail parsing the content.

One significant trade-off in this architecture is security. By acting as a proxy for HTTP-Basic authentication, you are effectively trusting the upstream server's identity based on the credentials provided by your PHP application. If your application shares these credentials with multiple endpoints, rotating them becomes a logistical burden. Additionally, Basic authentication transmits credentials in base64-encoded form over the wire unless explicitly wrapped in an HTTPS connection. While the example above enforces SSL verification, you must ensure that the connection between your proxy and the target URL is encrypted; otherwise, the authentication process becomes trivial to intercept.

For large-scale projects or those requiring more robust session handling, frameworks like Laravel provide a structured way to handle cURL operations via the Http facade or Guzzle HTTP clients. These abstractions often simplify error handling and middleware integration, making it easier to log proxy requests or apply rate limiting without rewriting low-level cURL options.

Our network