mgold
mgold

Reputation: 6366

PHP: Relative to absolute URLs in curl'ed html

I am retrieving a simple HTML page using curl in PHP:

<?php
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "http://example.com/server");
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_exec($ch);
 curl_close($ch);
?>

and includeing it in a file on my website:

<?php include('curlingCodeAbove.php');?>

I want to replace the relative URLs in the curled HTML with absolute URLs. I am familiar with Change a relative URL to absolute URL but I have tried unsuccessfully to insert that solution into this code.

Upvotes: 0

Views: 3958

Answers (2)

mgold
mgold

Reputation: 6366

Fixed it myself. Set the CURLOPT_RETURNTRANSFER flag.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/server");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://mydomain.com/$2$3', $result);
echo $result
?>

While it's probably bad form to accept your own answer, hey, I'm the one who figured it out.

Upvotes: 1

Bloafer
Bloafer

Reputation: 1326

The way I do it is search for ="/ and replace with ="http://www.example.com/

for example:

$replaces['="/'] = '="http://www.example.com/';
$output = str_replace(array_keys($replaces), array_values($replaces), $output);

I have not checked this but it should do what you need.

Upvotes: 0

Related Questions