Topic: fetch content from a remote file (Read 489 times)
Just another rainy day
Posts: 1
10 credits Members referred : 0
« on: Jun 24, 2006, 12:57:33 AM »
Hello, just learning php and have been impressed by it as a programming language. am really starting to enjoy learning it. I have been trying to write a script to fetch content from a remote file and display it locally - no real reason other than playing with php The script gets content of a remote page that holds links to news articles (example) shows the contents of the page with all the links and with the urls changed to point to a second script with a url queery holding target page url. on clicking a link it is passed onto the next script which just reads and displays the content of the target article page.
Have been trying to get it to work with a news site and having problems. Works a treat on my server getting files locally but seems to be having trouble with remote files - the script tries for a while and then returns an error. for some reason its having trouble finding start and end point of string but its possibly having trouble there from the file not being read properly. I read something about problems with packets and was wondering if thats the problem.
Can anybody see any obvious problems or give any advice on a better method of dealing with remote files - it took me a lot of hours to figure this out and would really like to see it finally work on a remote file and not just locally from localhost. (I only have half a grin on my face at this point)
script as follows(second script is essentialy same function without the url replacement and uses url queery info for target file):
Code:
<?php function report( $message, $report ) //error reporting function { if( $report!= FALSE ) echo( $message ); } //start getcontents function function getcontents( $fromUrl, $baseUrl, $startString, $endString, $report )
if ($fr) { $start = strpos( $fr, $startString ) + strlen($startString); $finish = strpos( $fr, $endString ); if( ($start!= FALSE) && ($finish!= FALSE) ) { $length = $finish - $start; //get contents of string from start point to end point $code = Substr( $fr, $start, $length );
//string replace domain name/ with news.php?id= so the original url can be passed to next script // and domain name does not appear in link - append domain name back on in next script //so it can fetch the news article.
$code = str_replace("http://www.somedomain.com/", "news.php?id=", strip_tags($code, "<br><img><a>")); //strip html tags but allow images, links and line breaks and replace url of target page with second script name and target url in url queery
echo str_replace("<a href", "<br><br><a href", $code);//put a couple of line breaks before every link and add above filename and url info to links //report errors } else report( "<! start/end string not found in $fromUrl >", $report ); } else report( "<! could not read data from $fromUrl >", $report ); } else report( "<! could not open $fromUrl >", $report ); }
?>
Code:
<?php //call to function getcontents( "http://www.somedomain.com/somefile.htm", "http://www.somedomain", "startpoint string text", "endpoint string text", TRUE );
?>
help much appreciated.
EDIT from Nikolas : Please post code fragments inside of a [ code ] bbcode tag
« Last Edit: Jun 24, 2006, 01:40:25 PM by Nikolas »
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6236
38242 credits Members referred : 374