呈现时,cURL Html输出与原始页面不同 [英] cURL Html output different from original page when rendered
本文介绍了呈现时,cURL Html输出与原始页面不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究一个涉及使用cURL或file_get_contents获取页面的项目。问题是,当我尝试回显提取的html时,输出看起来与原始页面不同,并不是所有图像都显示出来。请我想知道是否有解决方案。我的代码
<?php
//获取网址
$ url =http:/ /www.google.com;
$ b $ //获取url的html
函数get_data($ url)
{
$ ch = curl_init();
$ timeout = 5;
// $ userAgent =Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US)AppleWebKit / 525.13(KHTML,如Gecko)Chrome / 0.X.Y.Z Safari / 525.13。
$ userAgent =IE 7 - Mozilla / 4.0(兼容; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30);
curl_setopt($ ch,CURLOPT_USERAGENT,$ userAgent);
curl_setopt($ ch,CURLOPT_FAILONERROR,true);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ ch,CURLOPT_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;
}
$ html = file_get_contents($ url);
echo $ html;
?>
谢谢
解决方案您应该使用
< base>
来指定所有相关链接的基本网址: 如果你卷曲 http://example.com/thisPage.html
然后在你的回声输出中添加一个 base
标签''。这应该在技术上位于< head>
中,但这将起作用: echo'< base href =http://example.com//>';
echo $ html;
I am working on a project that involves fetching pages with cURL or file_get_contents. The problem is that when i try to echo the html fetched, the output seem to be different from the original page, not all images show up. Please i would like to know if there is a solution. My code
<?php
//Get the url
$url = "http://www.google.com";
//Get the html of url
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
//$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.X.Y.Z Safari/525.13.";
$userAgent = "IE 7 – Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)";
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_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);
return $data;
}
$html = file_get_contents($url);
echo $html;
?>
Thanks
解决方案
You should use <base>
to specify a base url for all relative links:
If you curl http://example.com/thisPage.html
then add a base
tag in your echoed output of ''. This should technically be in the <head>
, but this will work:
echo '<base href="http://example.com/" />';
echo $html;
Live example w <base>
is broken w/o <base>
这篇关于呈现时,cURL Html输出与原始页面不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文