Page 1 of 1

remote secured file reading in php

Posted: Mon Jul 20, 2009 1:00 pm
by varma457
Hi,

I have a problem with reading a secured remote file.I mean for reading a file required username && password..

I tried it this way..

$hange = fopen('http://username:password@www.fragrancen ... 2.txt','rt');

But is showing

failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required

help regarding this problem would be great

Re: remote secured file reading in php

Posted: Mon Jul 20, 2009 1:14 pm
by requinix
You sure you have the right file location? When I go to http://www.fragrancenet-wholesale.com/datafeed2.txt I'm redirected to http://www.fragrancenet.com/datafeed2.txt and that gives me a 404 page.

Re: remote secured file reading in php

Posted: Mon Jul 20, 2009 1:16 pm
by varma457

Re: remote secured file reading in php

Posted: Mon Jul 20, 2009 2:45 pm
by Darhazer
Use cURL instead. Otherwise you should implement HTTP authentication on your own, and cURL already support it...

Code: Select all

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);

Re: remote secured file reading in php

Posted: Mon Jul 20, 2009 3:05 pm
by varma457
can you give some example...

Re: remote secured file reading in php

Posted: Mon Jul 20, 2009 3:18 pm
by Darhazer

Code: Select all

$ch = curl_init($url);      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
 
$response = curl_exec($ch);
if ($response === false) {
    $error = curl_error($ch);
    curl_close($ch);
    die($error);
}
            
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode && $httpCode != 200) {
    curl_close($ch);
    die('Response code ' . $httpCode);
}
            
curl_close($ch);
            
echo $response;