Forum Moderators: coopster

Message Too Old, No Replies

how can i fetch a remote webpage like LWP

         

lindajames

1:55 pm on Apr 18, 2004 (gmt 0)

10+ Year Member



Hi,

I am used to using the Perl LWP module to fetch data from remote webpages. However, i need to know how this can be done in PHP. Can someone please tell me how i can fetch the entire content of a remote page using php and put the following at the begining of each new line:

document.write("

and put the following at the end of each line:

")

any help would be appreciated.

cheers

Yidaki

1:59 pm on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either use include:

include ("http[b]:[/b]//www.example.com/page.htm");

or curl, if you want to alter / parse the response:

$url = "http[b]:[/b]//www.example.com/page.htm";
$curl = curl_init ();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close($curl);
print $result;

lindajames

2:50 pm on Apr 18, 2004 (gmt 0)

10+ Year Member



the second one doesnt seem to work. however, i got this to work:

$open = fopen("http://www.example.com/page.htm", "r");
$read = fread($open, 9024);
fclose($open);

echo $read;

But i need to add something at the begining of each line and at the end of each line. I was thinking of using the str_replace function but cant seem to find a way of using it to do it.

any advice would be appreciated.

cheers

Yidaki

3:04 pm on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>the second one doesnt seem to work.

Yah, sorry that i didn't mention it. You'd need to install the Curl Library [curl.haxx.se].

>fopen
>need to add something at the begining of each line and at the end of each line.

You could use the str_replace or ereg_replace function to search and replace the returns \n.

$read = ereg_replace ("\n", "\ndocument.write('", $read);
$read = ereg_replace ("\n", "')\n", $read);

lindajames

3:40 pm on Apr 18, 2004 (gmt 0)

10+ Year Member



the ereg replace seems to be ok to put document.write(' in the begining of each line. But the line that reads: $read = ereg_replace ("\n", "')\n", $read); does seem to be very good as it adds ') one line above the document.write('

Isnt there any other functions to allow adding something to the end of each line?

Yidaki

4:04 pm on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>it adds ') one line above the document.write('

he he he, this just happens once per fetch - just remove the first replace again after you parsed the code.

$read = substr($read, 2)

This removes the first 2 chars "')".

gethan

4:17 pm on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try:

$lines = file("http://www.example.com/");

foreach ($lines as $line) {
// Do something. eg.
$line = "document.write('".$line."')";
//watch out for escaping quotes etc.
print $line;
}

or if you just want the whole file:

$html = join(file("http://www.example.com/"),'');
print $html;

Yidaki

4:25 pm on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



he he he, it needs a profi sometimes. Thanks gethan! Nice code.

lindajames

5:05 pm on Apr 18, 2004 (gmt 0)

10+ Year Member



thank you Yidaki and gethan for your help.

gethan, your code seems to do the trick. However, there is one problem. For some reason the code puts the ') on on a new line. so for example if I have a <tr> tag it will put it like this:

document.write('<tr>
')

i need the ') to move to the same line so it will end up looking like this

document.write('<tr>')

any ideas?

Cheers

gethan

5:22 pm on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The original file had the newlines so it includes them so use trim($line);

[hu.php.net...]

Strips out the whitespace, newlines, tabs etc from each line.

HTH

lindajames

5:39 pm on Apr 18, 2004 (gmt 0)

10+ Year Member



thank you. it seems to do the trick.