• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Getting text from remote URL

Hoth

NLC
NLC
In PHP, I have no trouble getting text from a local file and assigning it to a variable with fopen and fread... but as soon as I try to use it on a remote URL it stops working, even though the PHP manual says fopen should work on URLs.

When I use this, on any valid URL, the result of echoing $filecontents at the end is that it's blank:
PHP:
$fd = fopen($url, 'r');
$filecontents = fread ($fd, filesize($url));
fclose ($fd);

Somebody on another forum suggested trying fsockopen, but using that it still doesn't function. Again it doesn't read any data when I fsockopen... $ftest never takes on any value no matter what the URL when I use
PHP:
ftest = fsockopen ($url, 80, $errno, $errstr, 30);

Any ideas on how to read text from a URL?
 
Last edited:
My guess would be that filesize cannot determine the size of a remote file, so it's probably just returning 0. Try replacing filesize with a sufficiently large number so that you have something like:
PHP:
$fd = fopen($url, 'r');
$filecontents = fread ($fd, 1000000);
fclose ($fd);

Richard
 
Noldar - That seems to have been the problem, with that change it works perfectly. Thanks. :)

MindRash - Everyone has permission to read a regular web page, you can access URLs through PHP the same as you would through a web browser.
 
Back
Top