16, October 2008

fetch content from a remote file - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: fetch content from a remote file
« previous next »
Pages: [1] Print

Author Topic: fetch content from a remote file  (Read 549 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 )

{
$fd fopen$fromUrl"r" ); //open file from remote url
if( $fd!= FALSE )
{
$fr freadfopen$fromUrl"r" ), 200000 ); //read contents of file into string
fclose$fd );

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: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Jun 24, 2006, 09:05:52 AM »

Place the function at the begin of your script

Code:
error_reporting(E_ALL)
and tell us what is your error?
« Last Edit: Jun 24, 2006, 09:07:28 AM by olaf »


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8132
41749 credits
Members referred : 3



« Reply #2 on: Jun 24, 2006, 01:42:57 PM »

Why don't you try a preg or the explode() function?

If your problem is with fetching the data, try this Visit through proxy

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3024
Tags : php html domains articles programming Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: fetch content from a remote file
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 16, 2008, 05:39:23 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!





Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.