编程语言
首页 > 编程语言> > 肥皂xml通过php中的引用

肥皂xml通过php中的引用

作者:互联网

我在php中使用soap调用了Web服务,但是我从服务器收到xml中的错误作为响应.

问题是,在为请求创建xml时,Php在xml中引入了id,然后在找到相同节点的任何地方,它都将id作为引用传递.

例如:-

<ns1:ChargeBU id=\"ref1\">
<ns1:ChargeBreakUp>
<ns1:PriceId>0</ns1:PriceId>
<ns1:ChargeType>TboMarkup</ns1:ChargeType>
<ns1:Amount>35</ns1:Amount>
</ns1:ChargeBreakUp><ns1:ChargeBreakUp>
<ns1:PriceId>0</ns1:PriceId>
<ns1:ChargeType>OtherCharges</ns1:ChargeType>
<ns1:Amount>0.00</ns1:Amount>
</ns1:ChargeBreakUp>
</ns1:ChargeBU>

然后当找到相同的节点时

<ns1:ChargeBU href=\"#ref1\"/>

那么,如何防止这种情况,使它再次包含整个节点,而不仅仅是传递引用?

解决方法:

您可以创建该数组的新副本(实例),以防止php对相同的值使用引用.

例如,我们有:

$item = array(
    "id" => 1,
    "name" => "test value"
);

以及我们的要求/回应:

$response = array(
    "item1" => $item,
    "item2" => $item
);

默认情况下,php将使用对item1的引用替换item2的值(两个项目均指向同一数组)

为了防止这种行为,我们需要创建两个具有相同结构的不同项目,例如:

function copyArray($source){
    $result = array();

    foreach($source as $key => $item){
        $result[$key] = (is_array($item) ? copyArray($item) : $item);
    }

    return $result;
}

以及请求/响应:

$response = array(
    "item1" => copyArray($item),
    "item2" => copyArray($item)
);

相同的结构项实际上是内存中的不同数组,在这种情况下,php将不会生成任何引用

标签:soap-client,xml,php,soap
来源: https://codeday.me/bug/20191029/1960675.html