Make a function that generates the XML String and not the XML itself PHP [closed]

0
Hi, I'm trying to develop a function that generates only the XML string and not XML.

I'm trying to connect to a WebService that requires the following requirements:

user, password, codeAgency and XMLRequest.

The XMLRequest are the parameters and methods that I will call in webservice

These 4 requirements I pass by soapclient as follows:

array('usuarioCliente'=> $this->user,
      'senhaCliente'  => $this->password,
      'codigoAgencia' => $this->agencyCode,
      'xmlRequest' => $dados //parametros e metodos de todo o webservice
)

The problem is that the $ data must go in XML string format, without the 'xml version="1.0"' within xmlRequest appearing, if it pops up the WebService complains of the parameters and I am not able to develop a function that turn PHP keys into STRING XML

    
asked by anonymous 12.07.2016 / 20:42

2 answers

3

Doing this does not solve the problem?

function convertXML($dados)
{
    $xml = new SimpleXMLElement('<tag_header/>');
    array_walk_recursive($dados, array ($xml, 'addChild'));
    $dados = preg_replace('/<\?xml(.*)>/', '', $xml->asXML());
    $return $dados;
}

$saida = array('usuarioCliente'=> $this->user,
      'senhaCliente'  => $this->password,
      'codigoAgencia' => $this->agencyCode,
      'xmlRequest' => convertXML($dados); 
);
    
12.07.2016 / 22:36
0

Ivan, thank you!

But I did a function that answers me just what I need,

Follow the function I've developed.

private function toXml($array, $xml=""){
    $xml = $xml;
    foreach ($array as $chave => $valor) {
        if(is_array($valor)){
            $xml .= "<".preg_replace( '/\d+$/', null, $chave ).">".$this->toXml($valor)."</".preg_replace( '/\d+$/', null, $chave ).">";
        }
        else{
            if($chave == 'dadoAdicional1' || $chave == 'dadoAdicional2'){
                if($valor==""){
                    $xml .= "<$chave/>";
                }
                else{
                    $xml .= "<$chave>".$valor."</$chave>";
                }
            }
            else{
                if($valor==""){
                    $xml .= "<$chave/>";
                }
                else{
                    $xml .= "<".preg_replace( '/\d+$/', null, $chave ).">".$valor."</".preg_replace( '/\d+$/', null, $chave ).">";
                }
            }
        }
    }   
    return $xml;    
}

I made a Regex to take out the numbers of the keys and added an exception to fields that should not be removed.

Thank you very much for your response!

    
13.07.2016 / 20:49