Page 1 of 2
Grab source?
Posted: Tue Feb 03, 2004 6:36 am
by lazersam
Hi all
Anyone know how to grab the source code of a web site using PHP, and putting it into a string variable?
Thanks
Lawrence.

Posted: Tue Feb 03, 2004 7:24 am
by patrikG
You can grab the clientside source (HTML, CSS, Javascript etc.).
PHP is serverside, hence you can't.
Posted: Tue Feb 03, 2004 9:10 am
by kettle_drum
Just open the site with fopen and read the file into an array. As patrikG said the php is server side and so cant be grabbed - unless of course its a phps file.
Posted: Tue Feb 03, 2004 10:56 am
by lazersam
Sorry guys I didnt explained... what I meant was, how to grab source from any html page. I will be using a PHP script to do it with
I will try fopen and read.. thanks.
Lawrence.
Posted: Tue Feb 03, 2004 11:04 am
by malcolmboston
Just open the site with fopen and read the file into an array. As patrikG said the php is server side and so cant be grabbed - unless of course its a phps file.
Posted: Tue Feb 03, 2004 11:25 am
by lazersam
The trouble is that when I print the string variable to the screen the browser turns it back into a web-page...
Code: Select all
<?php
#Get Page
$grab=fopen("http://www.mysite.com/index.htm", "r");
$contents=fread($grab, 1000);
echo $contents ; # Displays html page but not source code
?>
Is there a way to tell the browser not to display the code as a web page?
Posted: Tue Feb 03, 2004 11:28 am
by malcolmboston
because you are echoing it it is writing the values back into php and therefore back onto the page
Posted: Tue Feb 03, 2004 11:28 am
by patrikG
use <xmp> ... </xmp>.
Posted: Tue Feb 03, 2004 11:31 am
by malcolmboston

whats that?

Posted: Tue Feb 03, 2004 11:33 am
by patrikG
magic
Posted: Tue Feb 03, 2004 11:35 am
by malcolmboston
roflmao, i looked in PHP manual XMP doesnt exist googled for <XMP> no matches exist?
share your magic?
Posted: Tue Feb 03, 2004 11:37 am
by lazersam
patrikG wrote:use <xmp> ... </xmp>.
patrik, I replaced the html tag with xmp using
Code: Select all
<?php
#Get Page
$grab=fopen("http://www.mywebsite.com/index.htm", "r");
$contents=fread($grab, 1000);
$contents = str_replace("html", "xmp", $contents);
echo $contents ;
?>
...and it worked ok - thanks

! Is that what you meant?
Lawrence.
Posted: Tue Feb 03, 2004 11:37 am
by patrikG
It's an html-tag.
Code: Select all
echo '<xmp>'.$sourceCodeFromAWebsite.'</xmp>';
Posted: Tue Feb 03, 2004 11:39 am
by patrikG
lazersam - nope, not quite. I am sorry I was a bit brief. With your source code:
Code: Select all
<?php
#Get Page
$grab=fopen("http://www.mywebsite.com/index.htm", "r");
$contents=fread($grab, 1000);
echo '<xmp>'.$contents.'</xmp>' ;
?>
Posted: Tue Feb 03, 2004 11:41 am
by lazersam
o i c
Thanks patrik.