Read another websites source code with php – PHP

  php

Q(Question):

Hey im new to php, anyway i have a website which will contain some information and will be updated regurly but rather than edit about 30 other websites as well for just the single page, i want to retrieve the source code of one website and view in all the others.

So far iv worked out how to output html code with php e.g.


$source = "<a href='http://www.google.com'> a link1</a>";
echo $source;

Any how i need to find out how to access a website and read its source code so that i can print it out on all other websites.

Hope this is clear enough if you need me to explain anymore ask;

Thanks for any help. it is greatly appreciated.

A(Answer):

Iv found some code that may achieve this but i get some errors that i can figure out –


$source=fopen("http://www.mywebsite.com/index.html", "r") or exit("Unable to open website!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file);
}
fclose($source);

The errors are – (test.php is php file i using to code in)

Warning: fopen(http://www.mywebsite.com/index.html): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home/content/d/a/t/*******/html/mywebsite/test.php on line 20

A(Answer):

Don’t Worry i figured out the problem or rather what works, thanks anyway

A(Answer):

Do you want to share your solution with our members?

Ronald

A(Answer):

It was terrible simple i had just forgot to name a varible the right thing


$source=fopen("http://www.mywebsite.com/index.html", "r") or exit("Unable to open website!");
while(!feof($source))
{
echo fgets($source);
}
fclose($source);

And that fixed the problem i was having

A(Answer):

Thanks for this. see you around.

Ronald

A(Answer):

Hi,
If i’m not wrong you can get the same output with following as well
[PHP]
$tempcontent = file_get_contents(‘http://search.yahoo.com/’);
echo $tempcontent;
[/PHP]

Regards,
RP

LEAVE A COMMENT