cURL基本身份验证

I'm trying to access a file from a distant server with

$fp = @fsockopen($ip,$port,$errno,$errstr,1);    
fputs($fp, "GET /randomfiletoget.html HTTP/1.0
User-Agent: Mozilla

");

But the server needs a HTML authentification. How should I pass the login/password to access the file?

Thanks!

To potential downvoters, I wrote authentification on purpose. Change Host to you domain name. It should return HTML output, with valid username and password.

function authentification($url, $username, $password){
    $headers = array(
    "Host=example.com",
    "User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11",
    "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language=en-us,en;q=0.5",
    "Accept-Encoding=gzip,deflate",
    "Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7",
    "Date: ".date(DATE_RFC822)
    );

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
    return curl_exec($curl);
}

As webarto mentioned in his comment, you should look into cURL, it supports basic HTTP auth among many other handy features.

For one, you should use Host header too. Todays hosting environments need it in 99%.

Basic http autentication is done with headers and Base64 encoding. This wikipedia article will tell you everíthing. Or use PHP's cURL functions.