使用PHP或JavaScript访问和打印HTML源代码 [英] Accessing and printing HTML source code using PHP or JavaScript

查看:129
本文介绍了使用PHP或JavaScript访问和打印HTML源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问并打印(或只是能够使用)任何使用PHP的网站的源代码。我不是很有经验,现在想我可能需要用JS来完成这个。到目前为止,下面的代码访问网页的源代码并显示网页...我想要它做的是显示源代码。从本质上讲,最重要的是,我希望能够将源代码存储在某种变量中,以便稍后使用它。并最终逐行阅读 - 但这可以稍后解决。

  $ url ='http:// www .google.com'; 
函数get_data($ url)
{
$ ch = curl_init();
$ timeout = 5;
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,$ timeout);
$ data = curl_exec($ ch);
curl_close($ ch);
返回$ data;
}
echo get_data($ url); //在这种情况下,print和echo会做同样的事情。


解决方案

考虑使用 file_get_contents() 而不是卷曲然后,您可以通过用& lt; 替换每个左括号(<),然后将其输出到页面, p>

 <?php 
$ code = file_get_contents('http://www.google.com');
$ code = str_replace('<','& lt;',$ code);
echo $ code;
?>

编辑:

看起来像是卷曲比FGC快,所以忽略这个建议。我的帖子的其余部分仍然存在。 :)

I am trying to access and then print (or just be able to use) the source code of any website using PHP. I am not very experienced and am now thinking I might need to use JS to accomplish this. So far, the code below accesses the source code of a web page and displays the web page... What I want it to do instead is display the source code. Essentially, and most importantly, I want to be able to store the source code in some sort of variable so I can use it later. And eventually read it line-by-line - but this can be tackled later.

$url = 'http://www.google.com';
function get_data($url) 
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
echo get_data($url); //print and echo do the same thing in this scenario.

解决方案

Consider using file_get_contents() instead of curl. You can then display the code on your page by replacing every opening bracket (<) with &lt; and then outputting it to the page.

<?php
$code = file_get_contents('http://www.google.com');
$code = str_replace('<', '&lt;', $code);
echo $code;
?>

Edit:
Looks like curl is actually faster than FGC, so ignore that suggestion. The rest of my post still stands. :)

这篇关于使用PHP或JavaScript访问和打印HTML源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆