PHP更改SoapClient的xml输出值[关闭]

问题描述 投票:0回答:2

我有一个Soap XML的PHP​​代码

<?php
$time = time();
$authcode = "code";
$client = new SoapClient('link',array("trace" => 1,"exceptions" => 0,'features' => SOAP_SINGLE_ELEMENT_ARRAYS));
$result=$client->GetCikkekAuth(array('web_update'=>date("Y-m-d",strtotime(' -1 day', $time)), 'authcode'=>$authcode));
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="utf-8"?>'."\n";
echo $result->GetCikkekAuthResult->any;
?>

这给我输出了一个很好的XML,我可以在WP woocommerce中导入。但是,XML中有一行处理产品的库存。

<cikk cikkid="15419">
..
<webmegjel>2</webmegjel>
..
</cikk>

问题是。 Woocommerce不会将该号码识别为有效的股票信息。 1 - 有货2 - 缺货。

我应该如何修改php代码以在最终的XML输出中更改所有2到outofstock和1-s to instock?

我知道如何更改物理XML中的值,但是这个值永远不会被保存,只会每天调用一次PHP。所以它必须在PHP运行时动态制作。

php xml if-statement soap soap-client
2个回答
0
投票

您可以对结果XML字符串执行文本替换: 将“<webmegjel> 1 <”替换为“<webmegjel> instock <” 将“<webmegjel> 2 <”替换为“<webmegjel> outofstock <”

例如:

<?php
$xmlString = '<cikk cikkid="15419">
<othertsuff>...</othertsuff>
<webmegjel>2</webmegjel>
<othertsuff>...</othertsuff>
<webmegjel>1</webmegjel>
</cikk>
';

$searchArray = array('<webmegjel>1<','<webmegjel>2<');
$replaceArray = array('<webmegjel>instock<','<webmegjel>outofstock<');    
$replacedString = str_replace($searchArray, $replaceArray, $xmlString);

echo '<pre>'.htmlentities($replacedString).'<pre>';
?>

0
投票

这样做了诀窍:

$response = $result->GetCikkekAuthResult->any;

$xml = simplexml_load_string($response);
$cikkek = $xml->xpath('/valasz/cikk');
foreach ($cikkek as &$cikk){
    switch ($cikk->webmegjel) {
        case '1': $cikk->webmegjel = 'instock'; break;
        case '2': $cikk->webmegjel = 'outofstock'; break;
        case '3': $cikk->webmegjel = 'outofstock'; break;
        default: $cikk->webmegjel = 'outofstock';
    }
}
header("Content-type: text/xml");
echo $xml->asXml();
© www.soinside.com 2019 - 2024. All rights reserved.