Grab source?

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

User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Grab source?

Post 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. :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

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 »

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.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

because you are echoing it it is writing the values back into php and therefore back onto the page
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

use <xmp> ... </xmp>.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

8O whats that? 8O
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

magic
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

roflmao, i looked in PHP manual XMP doesnt exist googled for <XMP> no matches exist?

share your magic?
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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 :D ! Is that what you meant?

Lawrence.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

It's an html-tag.

Code: Select all

echo '<xmp>'.$sourceCodeFromAWebsite.'</xmp>';
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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>' ;

?>
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

o i c

Thanks patrik.
Post Reply