Read source code with PHP

2019-02-07 15:15发布

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?

标签: php html http
4条回答
我只想做你的唯一
2楼-- · 2019-02-07 15:51

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.

查看更多
\"骚年 ilove
3楼-- · 2019-02-07 16:05

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

查看更多
神经病院院长
4楼-- · 2019-02-07 16:09

Try

<?php
$f = file_get_contents("http://www.site.com/f");
echo $f;
?>
查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-07 16:11

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;

?>
查看更多
登录 后发表回答