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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)