0

Is there away to read a source code from another site? Source code as HTML, when you right click on a site and then "Source code".

I've tried:

<? $f = fopen ("http://www.example.com/f", r);
echo $f;
?>

It did not work. How do I do this?

2
  • How didn't your posted code work? Commented Apr 22, 2011 at 16:15
  • @Kevin: Because f is a reference to the opened file Commented Apr 22, 2011 at 16:36

4 Answers 4

6

Try

<?php
$f = file_get_contents("http://www.site.com/f");
echo $f;
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Change echo $f to echo htmlspecialchars( $f ) and you'd view the actual source. Of course this all assumes that allow_url_fopen is enabled in ini.
Nick Weaver and Kevin Peno Thanks! This is what I was looking for! I'm grateful! I can't thank you enough. Thanks.
1

You could use curl to grab the remote HTML, and when printing I'd be sure to sanitize it so the viewing browser doesn't try to render or execute script. (after all, you can't guarantee to control the contents of the remote HTML - it could be malicious)

Alternatively you could use shell commands to grab it to a temporary file, and read that file in. Wget or the curl binary itself could be prodded into doing this for you. (wget -O /tmp/sometempfile http://www.site.com/f) but note this is dangerous, and might set off "alarms" for sysadmins watching the system. Calling wget et al and dumping in /tmp/ is generally the first thing someone tries to do when they break into a PHP application.

1 Comment

He was using fopen to download a website, I think your answer will only stand to confuse him.
1

That all depends on what you qualify as "source". To grab the output from a site, you could use curl:

$ch = curl_init('http://www.google.ca');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($ch);
echo $data;

If you're talking about the server-side source of the page, then no, there's no way that you can do that (I hope that's not what you're talking about :))

3 Comments

don't forget to use htmlspecialchars to see the source rather than rendering it.
Demian Brecht@ I got this error "Call to undefined function curl_init()". What is the difference between this and the first one posted? Thanks a lot of helping :D
@muazam: You need to have the curl mod installed. Do a quick google search for installing curl on whatever platform you're using.
0

For reading contents (source) of a remote page, at some other website:

<?php

$linktopage = "http://www.google.com/index.html";
$sourcecode = file_get_contents( $linktopage );
echo $sourcecode;

?>

4 Comments

How is this different from Nick's answer?
Considering the minimal time difference, it's not really a double post and the answer is correct :P
Nick's answer was at 3 points. It would be nice to avoid duplicates by looking at answered questions before answering.
Thanks it worked, but I think you forgot the htmlspecialchars function. :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.