Search code examples
phpxmlif-statementsoapsoap-client

PHP change xml output value of SoapClient


I have the following php code for a Soap XML

<?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;
?>

Which outputs me a nice XML that i can import in WP woocommerce. However there is a line in the XML that handles the Stock of the product.

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

The problem is. Woocommerce doesn't recognize the number as a valid stock information. 1 - in stock 2 - out of stock.

How should i modify the php code to change all the 2 to outofstock and the 1-s to instock in the final XML output?

I know how to change a value in a physical XML, but this one will never be saved, only the php will be called once a day. So itt has to be made on the fly while the php runs.


Solution

  • This done the trick:

    $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();