外部网页的HTML源代码为字符串 [英] HTML source code of external webpage as string

查看:101
本文介绍了外部网页的HTML源代码为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上的用户将在文本框中输入网址(另一个域中的公共网页)。我想以字符串的形式获取页面的HTML源代码。如何使用客户端脚本执行此操作?



以下是我所做的加载url的操作。但无法弄清楚如何获取html。

 <!DOCTYPE html> 
< head>
< script>
函数myFunction()
{
document.getElementById(site)。src = document.getElementById(web).value;
}
< / script>
< / head>
< body>
< input id =webtype =textname =user>
< input type =submitvalue =Submitonclick =myFunction()> <峰; br />
< iframe id =sitesrc =width =1200height =1200>< / iframe>

< / body>
< / html>


解决方案

在服务器上创建一个脚本, AJAX,然后使用 cURL 就像这样:

  $ ch = curl_init ); 
$ timeout = 10;
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 将包含内容。

A user on my website will enter a url (a public page on another domain) in a textbox. I want to fetch the HTML source code that page as a string. How to do this using client side scripting ?

Following is what I did to load url. But not able to figure out how to fetch html.

<!DOCTYPE html>
<head>
<script>
function myFunction()
{
document.getElementById("site").src=document.getElementById("web").value;
}
</script>
</head>
<body> 
<input id="web" type="text" name="user">
<input type="submit" value="Submit"  onclick="myFunction()"> <br/>
<iframe id="site" src="" width="1200" height="1200"></iframe>

</body>
</html>

解决方案

Create a script on your server, and call it using AJAX, then use cURL like so:

 $ch = curl_init();
 $timeout = 10;
 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);

And $data will contain the contents.

这篇关于外部网页的HTML源代码为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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