dscbxou1900343 2014-06-15 08:25
浏览 71
已采纳

stream_context_create,带有基于域名的HTTP选项

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?

  • 写回答

2条回答 默认 最新

  • drl959975 2014-06-15 08:45
    关注

    Perhaps you could proxy the request? I.e. document('//mydomain/?doc=origin-doc')

    This way you can modify the request (e.g. using cURL) to your needs. The doc parameter can be used to conditionally modify the request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器