tags:

views:

531

answers:

1

Hi,

I have a page that is making an XML POST-Request to an URL on a different server (e.g. "http://wwww.externalserver.com/login"). This server is sending plain HTML as response. The problem is, that the URL shown in the browser ist still on my local server, let's say "http://localhost/callExternal.php". The external URL is redirecting (302) to another page.

The returned HTML of this target page contains relative image paths. Because of the not matching URL these relative paths are not valid and therefore no images or stylesheets are loaded.

Is there a way to make CURL (or any other solution working in PHP4) changing the location so that these relative paths work (without parsing the HTML and changing the paths)?

This is the calling script "http://localhost/callExternal.php":

$data = '<?xml version="1.0" encoding="UTF-8"?><Some XML></Some XML>';  

$res = curl_init('http://wwww.externalserver.com/login');

curl_setopt($res, CURLOPT_POST, 1);
curl_setopt($res, CURLOPT_HEADER, 0);
curl_setopt($res, CURLOPT_POSTFIELDS, $data);
curl_setopt($res, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($res, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=UTF-8"));
curl_exec($res);
curl_close($res);

And this is a snippet of the returned HTML with a link example:

<html>
<head>
<link rel="stylesheet" type="text/css" media="print" href="/css/screen.css" />
</head>
<body>test</body>
</html>

And the URL is still showing "http://localhost/callExternal.php", so the link to the Stylesheet obviously doesn't work.

+1  A: 

Hi,

You might want to try inserting the <base> tag inside the <head> section of the HTML ; quoting w3schools :

The <base> tag specifies a default address or a default target for all links on a page.

The <base> tag goes inside the head element.


And, in the HTML 4 specifications :

In HTML, links and references to external images, applets, form-processing programs, style sheets, etc. are always specified by a URI. Relative URIs are resolved according to a base URI, which may come from a variety of sources. The BASE element allows authors to specify a document's base URI explicitly.

When present, the BASE element must appear in the HEAD section of an HTML document, before any element that refers to an external source. The path information specified by the BASE element only affects URIs in the document where the element appears.

The example given looks like this :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd"&gt;
<HTML>
 <HEAD>
   <TITLE>Our Products</TITLE>
   <BASE href="http://www.aviary.com/products/intro.html"&gt;
 </HEAD>

 <BODY>
   <P>Have you seen our <A href="../cages/birds.gif">Bird Cages</A>?
 </BODY>
</HTML>

And the relative URI "../cages/birds.gif" would resolve to:

http://www.aviary.com/cages/birds.gif
Pascal MARTIN
Thanks, I already thought about that, but the response HTML can't be changed because it's on an external server not under my control. I have to deal with what I got from the response.
acme
@acme: Change the HTML after you retrieve it on the server before sending it to the browser.
NickFitz
@acme : instead of directly outputting the content you get from your curl request, you have to fetch it into a PHP variable (see the CURLOPT_RETURNTRANSFER option) ;; then, you can use something like str_replace on that variable ;; and, finaly, echo its content to the user.
Pascal MARTIN
Is there no other way than parsing the returned HTML?
acme
Considering the HTML doesn't fit to what you need, I don't think you have much choice : to make it fit, you'll have to modify it (either par parsing it or just replacing somethig more brutaly)
Pascal MARTIN
You're right, it seems it can't be done elegantly. I set RETURNTRANSFER and adding a base-tag right after the head-Tag. This way it works at least, though it's obviously pretty ugly.
acme
Yep, it doesn't feel quite right ; but it should work, even if not looking great ^^
Pascal MARTIN
I solved it now by parsing the returned html and adding a base-tag to the header. It's ugly but it works ;-) Thanks!
acme
You're welcome :-)
Pascal MARTIN