具有绝对URL的cURL get_data($ url) [英] cURL get_data($url) with absolute URL

查看:204
本文介绍了具有绝对URL的cURL get_data($ url)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码通过cURL获取数据

I'm using this code to get data using cURL

$url='http://example.com/'; //URL to get content from..
print_r(get_data($url)); //Dumps the content

/* Gets the data from a URL */
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;
}

但是,此代码返回带有相对URL的数据。如何获得相对网址&用绝对网址打印?可能与preg_replace一起使用。但是如何?

However, This code returns data with relative url. How can I get ride of this relative url & print with absolute url? May be with preg_replace.. But How ?

推荐答案

看看HTML base 标签。如果要让浏览器执行所有相对到绝对的转换,将会发现它很有用:

Have a look at the HTML base tag. You should find it helpful if you want to let the browser do all the relative-to-absolute conversion:

$data = get_data($url);
// Note: ideally you should use DOM manipulation to inject the <base>
// tag inside the <head> section
$data = str_replace("<head>", "<head><base href=\"$url\">", $data);
echo $data;

这篇关于具有绝对URL的cURL get_data($ url)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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