View Full Version : problem with fread() on external URL
brothercake
09-12-2002, 06:29 PM
I'm trying to include the results of an Atomz search into a PHP template. The form uses GET to go to a result page on my site, which is then supposed to read the query string, append it to the Atomz search url, and read all that into a variable i can echo onto the page.
It seems to open the file okay but when I try to read it I get
Warning: stat failed ... (errno=2 - No such file or directory)
The file definitely exists, so I don;t know what's happening. I'm sure its my naivety that I've missed something obvious, but any thoughts are welcome. Here's the code:
<?
if(isset($_SERVER["QUERY_STRING"])) {
$filename = "http://search.atomz.com/search/?" . $_SERVER["QUERY_STRING"];
$fd = fopen($filename, "r");
$contents = fread($fd, filesize ($filename));
fclose($fd);
echo ($contents);
}
?>
Spookster
09-12-2002, 09:31 PM
So there is physically a file on the server named?
?" . $_SERVER["QUERY_STRING"];
When you use fopen it looks for a file at that location with that name. If it doesn't find one then you will get that error.
Why don't you just do it like this?
<?
if(isset($_SERVER["QUERY_STRING"])) {
include "http://search.atomz.com/search/?" . $_SERVER["QUERY_STRING"];
}
?>
Spookster
09-12-2002, 09:45 PM
oh BTW that feature doesn't work on windows versions of PHP so if you are developing this on windows with php such as PHPDEV you will get an error. Just test it on a linux server to see that it works.
You can also use require depending on how you want to handle errors.
brothercake
09-12-2002, 11:20 PM
I thought you can't use include on files from external servers?
Either way, I got it to work like this
if(isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"]) {
$filename = "http://search.atomz.com/search/?" . $_SERVER["QUERY_STRING"];
$fcontents = file ($filename);
while (list ($line_num, $line) = each ($fcontents)) {
echo $line, "\n";
}
}
thanks for the help
firepages
09-13-2002, 01:59 AM
I know you have it sorted but just a note that your original method failed as filesize() does not work on remote files
but you can read a line at a time..
<?
$fd = fopen ("http://somehost.com.page.page", "r");
while (!feof ($fd)) {
$yaks= fgets($fd, 4096);
echo $yaks;
}
fclose ($fd);
?>
Spookster
09-13-2002, 02:43 AM
Originally posted by brothercake
I thought you can't use include on files from external servers?
Either way, I got it to work like this
if(isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"]) {
$filename = "http://search.atomz.com/search/?" . $_SERVER["QUERY_STRING"];
$fcontents = file ($filename);
while (list ($line_num, $line) = each ($fcontents)) {
echo $line, "\n";
}
}
thanks for the help
Umm yes you can. I will upload an example.
This is using the same code I posted earlier:
http://www.designqueue.com/remotesearch.php
brothercake
09-14-2002, 04:08 AM
Originally posted by Spookster
This is using the same code I posted earlier:
:thumbsup:
firepages - I tried that technique as well, but it never finished loading, like it never found an EOF marker. I couldn't understand that one either. Thanks for the tip about no filesize info on remote files .. that makes sense to me at least :)
Ah well. It works now; that's the main thing. Thanks both for your help.
downsize
09-14-2002, 10:32 AM
http://www.designqueue.com/remotesearch.php <- hey looks good, but I am not Brothertwinkie :-P
Spookster
09-14-2002, 10:57 AM
Happy now? :D
http://www.designqueue.com/remotesearch.php
downsize
09-14-2002, 09:21 PM
that'll work
:thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.