Michael
Michael

Reputation: 479

Fix links on curl page

So after asking question after question people don't understand what I'm asking..

<?php

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://www.mybroadband.co.za/news/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);
curl_close($ch);

//Echo page back
echo $curl_response;

?>

Please run that script. In your address bar you'll see.. wwww.localhost/thisscriptname.php Now click a link. Look at your address bar. It redirects to www.mybroadbank.co.za/xyz

I don't want it to redirect there. I want it to get the link contents via curl and to display it at www.localhost/thisscriptnameoranyname.php

Please help. Thanks

Upvotes: 1

Views: 611

Answers (3)

user743234
user743234

Reputation:

That is because the cURL Response brings all the html code from that URL and applies into your current page. Thus, all the links returned to your page are linked to the destination url (which is http://www.mybroadband.co.za/). That results in something like this in the response:

<a href="http://www.mybroadband.co.za/xyz">XYZ</a>
<a href="http://www.mybroadband.co.za/abc">ABC</a>

Thus, when you click on these links, it will direct you to http://www.mybroadband.co.za/something. What you need is not to be redirected into those pages, but instead display the content in your localhost page? Assuming that I'm right.

So, in order to solve this problem, you need to edit the cURL Response in which you transform those HTML anchor tags (links) above into:

<a onClick="getPage('http://www.mybroadband.co.za/xyz')">XYZ</a>
<a onClick="getPage('http://www.mybroadband.co.za/abc')">ABC</a>

Then just code a getPage($link) function in Javascript, using AJAX to request the page from $link and return, just like what you did.

Upvotes: 1

Marc B
Marc B

Reputation: 360712

Remove the <base> tag from the header of the retrieved text. That's causing all the links to be based off the original site, not your proxied version:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
    <head>
    <base href="http://mybroadband.co.za/news/" />
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- remove this line

Though, if you're just blindly passing through the text without doing anything, you should probably just put this into an iframe and save the bandwidth on your server.

Upvotes: 0

Mel
Mel

Reputation: 6157

To explain what's going on: It does exactly what you want. But the browser interprets the HTML so you see the page. If you want to see the actual HTML code, then use echo htmlspecialchars($curl_response);. Putting it in a text area might not produce the correct results.

If you want to replace all links within the HTML code so that it stays on your page, then it's a whole different ballgame. You will need to parse the HTML code and replace them, using things like preg_replace and str_replace.

Upvotes: 0

Related Questions