Read source code with PHP

前端 未结 4 1360
后悔当初
后悔当初 2021-01-01 09:10

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:

<         


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

    Try

    <?php
    $f = file_get_contents("http://www.site.com/f");
    echo $f;
    ?>
    
    0 讨论(0)
  • 2021-01-01 10:09

    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 :))

    0 讨论(0)
  • 2021-01-01 10:12

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

    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.

    0 讨论(0)
提交回复
热议问题