php file_get_contents() shows page instead of html source code

后端 未结 4 884
春和景丽
春和景丽 2021-01-03 09:42

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;


        
相关标签:
4条回答
  • 2021-01-03 10:23

    Try this, using htmlspecialchars:

    $homepage = file_get_contents('http://homepage.com');
    echo htmlspecialchars($homepage);
    
    0 讨论(0)
  • 2021-01-03 10:27

    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;
    
    0 讨论(0)
  • 2021-01-03 10:40

    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.

    0 讨论(0)
  • 2021-01-03 10:41

    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);
    
    0 讨论(0)
提交回复
热议问题