3

I've got this piece of code, which should fetch the source code of the website.

$homepage = file_get_contents('http://homepage.com');
echo $homepage;

Instead of actually giving me the source code.. It's shows me the page that I'm trying to get the source code from.

1
  • Have a look on the page source and think for a second Commented Sep 16, 2013 at 23:09

4 Answers 4

19

Either use htmlentities or change the content type.

$homepage = file_get_contents('http://homepage.com');
echo htmlentities($homepage);

or

header('Content-type: text/plain');
$homepage = file_get_contents('http://homepage.com/');
echo $homepage;
Sign up to request clarification or add additional context in comments.

1 Comment

May I ask what you're intending to do with the fetched HTML code? If you want to display the code on a web page with other content/design or you want to drop it into a textarea on a form, then you'll need to use htmlentities. If you set the header, then you won't be able to display anything else on your resultant page other than text, because it's basically doing the same thing as taking a page called index.html and renaming it to index.txt -- it won't be parsed as code anymore.
1

Try this, using htmlspecialchars:

$homepage = file_get_contents('http://homepage.com');
echo htmlspecialchars($homepage);

1 Comment

the htmlspecialchars didn't do the job for me. Thanks for taking your time to help me out.
1

That's because you're fetching the source code and (re)outputting it. Your page is just mirroring http://homepage.com.

To see the actual page source, add a Content-Type header before your echo statement:

header('Content-type: text/plain');

This tells the browser treat the source as plain text and not interpret it as HTML.

1 Comment

This works well for me as well. Thanks! But, i'm wondering if there is any solution to display the html code as well as php code if that web page has php code in it? Just curios! Because i created a simple login website. Afraid that there is any solution can view the php code i have in the page. So that people can easily hack the login details.
0

It's because you are printing the source, your browser will interpret that as a webpage. To see the actual code use:

echo htmlspecialchars($homepage);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.