View Full Version : Error when retrieving file contents

03-18-2009, 10:54 PM
Warning: file_get_contents(http://calltrack/xxx/xxx.asp?custid=16xx79) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

I get this error when trying to pull contents from this location, which is a customer database on our intranet. I was told by another associate that this is because it relies on the browser to authenticate my enviroment, and since its only being sent by the server it can't do this so I am rejected. I tried doing this further using curl, but I don't understand this well. Looking for someone who understands what I'm encountering and might offer some sort of insight towards a resolution. Would I need too use curl to pass the correct header/additional information (picking at straws here, I really don't understand curl processing past what I've written.)

03-19-2009, 01:31 AM
It looks like a server setup issue with PHP... you probably probably want to talk to the server admin.

03-19-2009, 02:05 AM
It simply needs authorization, if you have username and password this script should do the trick:

// Initialise cURL session
$curl = curl_init();

// Specify the username/password to use
$user = "username";
$password = "password";

// URL to access
$url = "http://calltrack/xxx/xxx.asp?custid=16xx79";

// Set cURL parameters for this transaction
// Load in the destination URL
curl_setopt($curl,CURLOPT_URL, $url);
// Place a nice friendly user-agent
// Return the output instead of displaying it
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
// Don't return header
curl_setopt($curl,CURLOPT_HEADER, false);
// Provide credentials
curl_setopt($curl,CURLOPT_USERPWD,$user . ":" . $password);
// Follow on redirect
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);

// execute, and log the result to curl_put.log
$result = curl_exec($curl);
$error = curl_error($curl);
echo $result;
$return = "Error";
$message = curl_error();

// Close cURL session

03-19-2009, 05:57 PM
Thank you Steelaz! This is what I was looking for, I couldn't find the proper way to pass authentication... my only other question is any familiar with a way to prompt for username/pw that could be stored, not in plaintext for use with this method. Basically I wrote a scraping program to return accounts based on fairly unrelated ID's and at this point the associate using it would have to enter their corp ID + pw to access our database... just a nudge in the right direction would be great if this is more complex then a simple c/p, I'm just not sure where to start even.