1

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;
}

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 ?

MANnDAaR
  • 2,546
  • 7
  • 45
  • 64

2 Answers2

5

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;
Salman A
  • 262,204
  • 82
  • 430
  • 521
1

I think that you must to use a HTML parser like http://simplehtmldom.sourceforge.net/, and replace all links with the correct path.

TlmaK0
  • 3,578
  • 2
  • 31
  • 51