How can I tell the streamContext, created with stream_context_create()
to use different HTTP Headers according to the domain name of a requested URI?
I'm using XSL to transform an XML on server side (PHP).
To do so, I first create a stream context with some options and apply this stream to libxml
:
$streamContextOptions = array(
'http' =>array(
'header'=>$clientHeadersString /* HTTP header */
)
);
$streamContext = stream_context_create($streamContextOptions);
libxml_set_streams_context($streamContext);
Then I load the XML, XSL and do the transform
// Load the XML
$xmlDocument = new DOMDocument();
$xmlDocument->load($xmlURI);
// Load the XSL into
$xslDocument = new DOMDocument();
$xslDocument->load($xslURI);
$xslProcessor = new XSLTProcessor();
$xslProcessor->importStylesheet($xslDocument);
// Apply the XSL
$htmlDocument = $xslProcessor->transformToDoc($xmlDocument);
In the XSL, I have some document()
calls so the PHP server calls the specified URIs using the stream context. But I have different domains inside these document()
calls (let's say I have www.foo.com and www.bar.com).
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:value-of select="document('http://www.foo.com/with-user-agent.xml.php')/root"/>
<xsl:value-of select="document('http://www.bar.com/with-accept-language.php')/root"/>
</xsl:template>
</xsl:stylesheet>
What I want is to change the HTTP headers the libxml is using when doing the document()
calls inside the XSL, so I can send [cookies + user-agent] to foo.com, and [other cookies + accept-language] to bar.com when they are requested inside an XSL document()
call.
I don't want to send "too many" header to foo.com (aka, I don't want to send Accept-Language
to foo.com) nor to bar.com, and I don't want the cookies to mix (cookies send to foo.com must not be send to bar.com).
Somehow, like:
$streamContextOptions = array(
'http-for-foo' =>array(
'header'=>$clientHeadersStringForFoo /* HTTP header only when document() calls for foo.com*/
),
'http-for-bar' =>array(
'header'=>$clientHeadersStringForBar /* HTTP header only when document() calls for bar.com*/
)
);
Is there a way to tell XSLTProcessor to change the streamOptions depending on the domain requested by document()
function?