Using Curl With PHP to Retrieve Pages With Basic Authentication

To retrieve a particular page from another web site using basic authentication, use this PHP script:

<?php
$path = $_GET['path'];
$username = "username";
$password = "password";
$uri = "https://example.com/$path";
$ch = curl_init($uri);
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER  =>true,
    CURLOPT_USERPWD=> "$username:$password",
    CURLOPT_HTTPAUTH=> CURLAUTH_ANY,
    CURLOPT_VERBOSE     => 1
));
$out = curl_exec($ch);
curl_close($ch);
echo $out;
?>

Use the path of the file as the parameter. In this case you could retrieve an HTML file by a URL like this:

https://yoursite.com/proxy.php?path=path/to/file.html

Beware, because this isn’t particularly secure, but it is a way to do some quick and dirty proxy for servers with known users and where the script is hosted on a very secure site, but is consuming from another site that has lighter security requirements, like basic authentication.