remote secured file reading in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
varma457
Forum Newbie
Posts: 23
Joined: Sat Jul 11, 2009 2:43 pm

remote secured file reading in php

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: remote secured file reading in php

Post 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.
varma457
Forum Newbie
Posts: 23
Joined: Sat Jul 11, 2009 2:43 pm

Re: remote secured file reading in php

Post by varma457 »

User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: remote secured file reading in php

Post 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);
varma457
Forum Newbie
Posts: 23
Joined: Sat Jul 11, 2009 2:43 pm

Re: remote secured file reading in php

Post by varma457 »

can you give some example...
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: remote secured file reading in php

Post 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;
Post Reply