开发者

Read source code with PHP

Is there away to read a source code from another site? Source code as HTML, when you right click on a site and then "Sour开发者_如何学Pythonce code".

I've tried:

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

It did not work. How do I do this?


Try

<?php
$f = file_get_contents("http://www.site.com/f");
echo $f;
?>


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜