0

I have the following PHP code from which I need to generate XML.

$hparams["SiteName"]=""; 
$hparams["AccountCode"]=""; 
$hparams["UserName"]='xxxx'; 
$hparams["Password"]='xxxx'; 
$client_header = new SoapHeader('url','AuthenticationData',$hparams,false); 
$cliente = new SoapClient($wsdl); $cliente->__setSoapHeaders(array($client_header)); 
$opta=array(); 
$opta["Search"]["request"]["Origin"]="MAA"; 
$opta["Search"]["request"]["Destination"]="BOM"; 
$opta["Search"]["request"]["DepartureDate"]="2014-05-20T00:00:00"; 
$opta["Search"]["request"]["ReturnDate"]="2014-05-22T00:00:00"; 
$opta["Search"]["request"]["Type"]="OneWay"; 
$opta["Search"]["request"]["CabinClass"]="All";
$opta["Search"]["request"]["PreferredCarrier"]=""; 
$opta["Search"]["request"]["AdultCount"]="1"; 
$opta["Search"]["request"]["ChildCount"]="0"; 
$opta["Search"]["request"]["InfantCount"]="0"; 
$opta["Search"]["request"]["SeniorCount"]="0";
$opta["Search"]["request"]["IsDirectFlight"]="true"; 
$opta["Search"]["request"]["PromotionalPlanType"]="Normal";
$h=array();      
$h= (array)$cliente->__call('Search',$opta); 

How can I generate an XML of the above variables in PHP ?

The format should be

<xml>
<credential>
<Sitename>sitename</Sitename>
<AccountCode>ACC Code</AccountCode>
</credentials>

<Data>
<Origin>MAA</Origin>
<Destination>BOM</Destination>
</Data>
</xml>

Any help would be appreciate.

Thank you.

2
  • Can you share url of wsdl? Commented Apr 12, 2014 at 12:15
  • Is passing array as parameters doesn't work? Commented Apr 12, 2014 at 12:28

2 Answers 2

2
<?php 
ini_set('error_reporting', E_ALL);
$dom = new DomDocument('1.0');  // making xml

$credentials    = $dom->appendChild($dom->createElement('Credentials'));  // adding root element <credentials>

$sitename       = $credentials->appendChild($dom->createElement('Sitename')); // adding element  <sitename> in <credentials> 
$accountcode    = $credentials->appendChild($dom->createElement('AccountCode')); // adding element  <accountcode> in <credentials> 

$sitename->appendChild($dom->createTextNode('sitename')); // adding text in <sitename> 
$accountcode->appendChild($dom->createTextNode('ACC Code')); // adding text in <accountcode> 

$data           = $dom->appendChild($dom->createElement('Data')); 
$origin         = $data->appendChild($dom->createElement('Origin')); 
$destination    = $data->appendChild($dom->createElement('Destination'));

$origin->appendChild($dom->createTextNode('MAA')); 
$destination->appendChild($dom->createTextNode('BOM')); 

$dom->formatOutput = true; // generating xml

// generating XML as string or file 
$test1 = $dom->saveXML(); 
$dom->save('test1.xml'); 
?>

I think you could write loop by yourself ;) P.S. PHP 5+

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, your xml is not well structured.
It should be like:

    <xml>
        <credentials>
            <Sitename>sitename</Sitename>
            <AccountCode>ACC Code</AccountCode>
            <Data>
                <Origin>MAA</Origin>
                <Destination>BOM</Destination>
            </Data>
        </credentials>
<credentials>
    ...
</credentials>
    </xml>

Iterate the obtained result, and by concating , form the needed xml.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.