PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Tue Feb 03, 2004 6:36 am
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.
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Tue Feb 03, 2004 7:24 am
You can grab the clientside source (HTML, CSS, Javascript etc.).
PHP is serverside, hence you can't.
kettle_drum
DevNet Resident
Posts: 1150 Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England
Post
by kettle_drum » Tue Feb 03, 2004 9:10 am
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.
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Tue Feb 03, 2004 10:56 am
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.
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Feb 03, 2004 11:04 am
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.
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Tue Feb 03, 2004 11:25 am
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?
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Feb 03, 2004 11:28 am
because you are echoing it it is writing the values back into php and therefore back onto the page
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Tue Feb 03, 2004 11:28 am
use <xmp> ... </xmp>.
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Tue Feb 03, 2004 11:33 am
magic
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Feb 03, 2004 11:35 am
roflmao, i looked in PHP manual XMP doesnt exist googled for <XMP> no matches exist?
share your magic?
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Tue Feb 03, 2004 11:37 am
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.
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Tue Feb 03, 2004 11:37 am
It's an html-tag.
Code: Select all
echo '<xmp>'.$sourceCodeFromAWebsite.'</xmp>';
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Tue Feb 03, 2004 11:39 am
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>' ;
?>
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Tue Feb 03, 2004 11:41 am
o i c
Thanks patrik.