Asked  9 Months ago    Answers:  5   Viewed   116 times

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>example2@example2.com</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

 Answers

14

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.

Wednesday, March 31, 2021
 
Anand
answered 9 Months ago
81

PHP's SoapClient has what is called "WSDL mode", where when you give it the URL for the WSDL, it downloads that WSDL, and extracts the real end point URL from the WSDL.

Java's SOAPConnection does not have a "WSDL mode", so you need to provide the real end point URL to the call() method, not the WSDL URL.

If you don't know the real end point URL, do what SoapClient does, download the WSDL yourself and look at it. The end point URL will be at the end.

From example WSDL:

  ...

  <service name="EndorsementSearchService">
    <documentation>snowboarding-info.com Endorsement Service</documentation> 
    <port name="GetEndorsingBoarderPort"
          binding="es:EndorsementSearchSoapBinding">
      <soap:address location="http://www.snowboard-info.com/EndorsementSearch"/>
    </port>
  </service>

</definitions>

Here the real end point URL is: http://www.snowboard-info.com/EndorsementSearch

Wednesday, March 31, 2021
 
VieStar
answered 9 Months ago
16

Remove the whitespace before <?xml and then it works. If you can't, maybe you can alter/extend the nusoap class to trim the response before interpretation. Most likely the extra whitespace comes from some accidental whitespace outside the <?php & ?> tags.

Friday, May 28, 2021
 
dkcwd
answered 7 Months ago
55

The real reason is normalisation, and you will break the first normalform by doing it.

However, there are many cases in which a breach of the normal forms could be considered. How many fields are you dealing with and are they all booleans?

Storing an array serialized as a string in your database will have the following disadvantages (among others):

  • When you need to update your settings you must first extract the current settings from the database, unserialize the array, change the array, serialize the array and update the data in the table.
  • When searching, you will not be able to just ask the database whether a given user (or a set of users) has a given setting disabled or enabled, thus you won't have any chances of searching.

Instead, you should really consider the option of creating another table with the records you need as a one-to-many relation from your other table. Thus you won't have 30 empty fields, but instead you can just have a row for each option that deviates from the default (note that this option has some disadvantages aswell, for example if you change the default).

In sum: I think you should avoid serializing arrays and putting them into the databases, at least if you care just a tiny bit about the aforementioned disadvantages.

Saturday, May 29, 2021
 
Hexaholic
answered 7 Months ago
70

If anyone should be interested, i have provided the correct answer:

$soapUrl = "http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint";

$xml_post_string = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/"><customerID>XXX</customerID><key>XXXXXX-XXXXXX</key><serviceID></serviceID><paramID>0</paramID><address>RiksvÅ gen 5</address><postcode>59018</postcode><city>Mantorp</city><maxhits>10</maxhits></SearchCollectionPoint></soap12:Body></soap12:Envelope>';

$headers = array(
"POST /package/package_1.3/packageservices.asmx HTTP/1.1",
"Host: privpakservices.schenker.nu",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
); 

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch); 
curl_close($ch);

$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);

$parser = simplexml_load_string($response2);
Wednesday, August 11, 2021
 
nhunston
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share