Reputation: 83
I want to convert a soap xml response and store it in a database. Here is the XML that I have.
<ENV:Envelope xmlns:ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/soap/example">
<ENV:Body>
<ns1:GetCentresResponse>
<ExampleCentre>
<ns1:Cent>
<ID>200</ID>
<Name>example2</Name>
<Code>ex2</Code>
<Email>[email protected]</Email>
<Address1>example2, example2 </Address1>
<Address2>example2, example2 </Address2>
<City>example2</City>
<PostCode>111111</PostCode>
<Telephone>1111111111</Telephone>
<Location>11.11,-11.11</Location>
<URL>/example2/exam2/ex2</URL>
</ns1:Cent>
</ExampleCentre>
</ns1:GetCentresResponse>
</ENV:Body>
</ENV:Envelope>
I get this soap response from the server. I want to convert this to a array and store it in database. What should I do? I know the answer might be pretty straight forward, but hey, am a newbie :D
Would really appreciate any help I get.
Thank you in anticipation.
Regards
Upvotes: 1
Views: 15222
Reputation: 6036
Parse SOAP response to an Array using following code:
You just have to call function with SOAP-XML. After that it will return a Plain XML, then you must convert it to an array using JSON encode-decode.
$plainXML = mungXML($soapXML);
$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
// FUNCTION TO MUNG THE XML SO WE DO NOT HAVE TO DEAL WITH NAMESPACE
function mungXML($xml)
{
$obj = SimpleXML_Load_String($xml);
if ($obj === FALSE) return $xml;
// GET NAMESPACES, IF ANY
$nss = $obj->getNamespaces(TRUE);
if (empty($nss)) return $xml;
// CHANGE ns: INTO ns_
$nsm = array_keys($nss);
foreach ($nsm as $key)
{
// A REGULAR EXPRESSION TO MUNG THE XML
$rgx
= '#' // REGEX DELIMITER
. '(' // GROUP PATTERN 1
. '\<' // LOCATE A LEFT WICKET
. '/?' // MAYBE FOLLOWED BY A SLASH
. preg_quote($key) // THE NAMESPACE
. ')' // END GROUP PATTERN
. '(' // GROUP PATTERN 2
. ':{1}' // A COLON (EXACTLY ONE)
. ')' // END GROUP PATTERN
. '#' // REGEX DELIMITER
;
// INSERT THE UNDERSCORE INTO THE TAG NAME
$rep
= '$1' // BACKREFERENCE TO GROUP 1
. '_' // LITERAL UNDERSCORE IN PLACE OF GROUP 2
;
// PERFORM THE REPLACEMENT
$xml = preg_replace($rgx, $rep, $xml);
}
return $xml;
}
print_r($arrayResult);
Upvotes: -1
Reputation: 86
The best solution would be to use PHP's SoapClient class to do the call which will return you an object and then converting this object to an array, like so:
<?php
$client = new SoapClient("http://localhost/code/soap.wsdl");
// Soap call with HelloWorld() method
$something = $client->HelloWorld(array('option1' => 'attribute1'));
// Convert object to array
$array = (array)$something;
?>
Which you can then store in the database.
Upvotes: 3
Reputation: 10582
If you can't use SoapClient
to retrieve the SOAP response in a PHP object, then use SimpleXML to parse the soap response.
For example (where $xmlstr
contains the SOAP response):
$element = new SimpleXMLElement( $xmlstr );
$centerElement = $element->Body->GetCentresResponse->ExampleCentre->Cent;
$center = array(
$centerElement->ID,
$centerElement->Name,
$centerElement->Code,
$centerElement->Email,
$centerElement->Address1,
$centerElement->Address2,
$centerElement->City,
$centerElement->PostCode,
$centerElement->Telephone,
$centerElement->Location,
$centerElement->URL,
);
Now you can store $center
in the database.
Upvotes: 1