Jump to content

How to parse this SOAP request


dr.pepper

Recommended Posts

I am looking for some help parsing the SOAP request below:

 

try
{
    $aSoapConnectionOptions = array(
        'cache_wsdl'    => WSDL_CACHE_BOTH,
        'compression'   => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
        'encoding'      => 'ISO-8859-1',
        'trace'         => 1
    );

    $client = new SoapClient(WSDL_URI, $aSoapConnectionOptions);

$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ReceiveData xmlns="GDWebService">
      <value>
	<![CDATA[
	<?xml version="1.0" encoding="utf-8"?>
                    <XMLRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                            <Description>EventsOnlyListLoad</Description>
                            <Item xsi:type="XMLEventsOnlyRequest">
                            <RangeStartDate>01/01/2006</RangeStartDate>
                            <RangeEndDate>01/12/2012</RangeEndDate>
                            <ResType />    
                            <ResView>Web Sales Events</ResView>
                            </Item>
                        </XMLRequest>
	]]>
        </value>
    </ReceiveData>
  </soap:Body>
</soap:Envelope>';

$response = $client->__doRequest($xml,REQUEST_URL,"GDWebService/ReceiveData",'1.1');

 

When I print out the response, I get the following:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ReceiveDataResponse xmlns="GDWebService"><ReceiveDataResult><?xml version="1.0"?>
<ResourceList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ResList>
    <anyType xsi:type="XMLEventsOnly">
      <AVAILABLE>0</AVAILABLE>
      <EVENTNAME>Test Event</EVENTNAME>
      <RESTYPE>Families</RESTYPE>
      <TotalBooked>0</TotalBooked>
      <StartTime>11:00</StartTime>
      <ID>0069</ID>
      <EventCode>0000003701</EventCode>
      <EventDate>30/05/2011</EventDate>
      <EventDescription> </EventDescription>
      <Capacity>0</Capacity>
      <ProductMapID>   </ProductMapID>
      <LongDescription> </LongDescription>
      <ImageLink> </ImageLink>
      <MINPERBOOKING>0</MINPERBOOKING>
      <MAXPERBOOKING>0</MAXPERBOOKING>
      <ValidFrom> </ValidFrom>
      <ValidTo> </ValidTo>
      <AdditionalInfo> </AdditionalInfo>
      <ValidDays> </ValidDays>
      <MoreInfoLink> </MoreInfoLink>
    </anyType>
</ResList>
  <EventsOnly>false</EventsOnly>
</ResourceList></ReceiveDataResult></ReceiveDataResponse></soap:Body></soap:Envelope>

 

I want to be able to get the response as an object so I can easily loop through the ResList. Currently, the only way I seem to be able to do this is by doing the following but I know it's not the best code:

 

$response = str_replace("xmlns=\"GDWebService\"", "", $response);
                        $response = str_replace("soap:Body","body",$response);
            
                        $response = simplexml_load_string($response);
                        
                        $response = $response->body->ReceiveDataResponse->ReceiveDataResult;
                        
                        $response = simplexml_load_string($response);
                       
                        if($response->ResList)
                        foreach($response->ResList->anyType as $Item){
                       //echo data here
                        }

 

Hope someone can help.

 

Thanks

Link to comment
Share on other sites

I understand that. Just to confirm, if you had a value like...

<EVENTNAME>Test <error> Event</EVENTNAME>

... it would end up like

<EVENTNAME>Test &lt;error&gt; Event</EVENTNAME>

 

If so...

<?php 

$str = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ReceiveDataResponse xmlns="GDWebService"><ReceiveDataResult><?xml version="1.0"?>
<ResourceList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ResList>
    <anyType xsi:type="XMLEventsOnly">
      <AVAILABLE>0</AVAILABLE>
      <EVENTNAME>Test &lt;error&gt; Event</EVENTNAME>
      <RESTYPE>Families</RESTYPE>
      <TotalBooked>0</TotalBooked>
      <StartTime>11:00</StartTime>
      <ID>0069</ID>
      <EventCode>0000003701</EventCode>
      <EventDate>30/05/2011</EventDate>
      <EventDescription> </EventDescription>
      <Capacity>0</Capacity>
      <ProductMapID>   </ProductMapID>
      <LongDescription> </LongDescription>
      <ImageLink> </ImageLink>
      <MINPERBOOKING>0</MINPERBOOKING>
      <MAXPERBOOKING>0</MAXPERBOOKING>
      <ValidFrom> </ValidFrom>
      <ValidTo> </ValidTo>
      <AdditionalInfo> </AdditionalInfo>
      <ValidDays> </ValidDays>
      <MoreInfoLink> </MoreInfoLink>
    </anyType>
</ResList>
  <EventsOnly>false</EventsOnly>
</ResourceList></ReceiveDataResult></ReceiveDataResponse></soap:Body></soap:Envelope>';

$str = htmlspecialchars_decode( $str );
// Cut out what we don't need from the string, avoid processing redundant info that could fail
$start = strpos( $str, '<ResourceList' );
$end = strpos( $str, '</ResourceList>'  );
$str = substr( $str, $start, $end-$start+15 );

$xml = new SimpleXMLElement( $str, LIBXML_NOERROR | LIBXML_NOWARNING  );
print_r( $xml );
// VICTORY

?>

Link to comment
Share on other sites

I understand that. Just to confirm, if you had a value like...

<EVENTNAME>Test <error> Event</EVENTNAME>

... it would end up like

<EVENTNAME>Test &lt;error&gt; Event</EVENTNAME>

 

If so...

<?php 

$str = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ReceiveDataResponse xmlns="GDWebService"><ReceiveDataResult><?xml version="1.0"?>
<ResourceList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ResList>
    <anyType xsi:type="XMLEventsOnly">
      <AVAILABLE>0</AVAILABLE>
      <EVENTNAME>Test &lt;error&gt; Event</EVENTNAME>
      <RESTYPE>Families</RESTYPE>
      <TotalBooked>0</TotalBooked>
      <StartTime>11:00</StartTime>
      <ID>0069</ID>
      <EventCode>0000003701</EventCode>
      <EventDate>30/05/2011</EventDate>
      <EventDescription> </EventDescription>
      <Capacity>0</Capacity>
      <ProductMapID>   </ProductMapID>
      <LongDescription> </LongDescription>
      <ImageLink> </ImageLink>
      <MINPERBOOKING>0</MINPERBOOKING>
      <MAXPERBOOKING>0</MAXPERBOOKING>
      <ValidFrom> </ValidFrom>
      <ValidTo> </ValidTo>
      <AdditionalInfo> </AdditionalInfo>
      <ValidDays> </ValidDays>
      <MoreInfoLink> </MoreInfoLink>
    </anyType>
</ResList>
  <EventsOnly>false</EventsOnly>
</ResourceList></ReceiveDataResult></ReceiveDataResponse></soap:Body></soap:Envelope>';

$str = htmlspecialchars_decode( $str );
// Cut out what we don't need from the string, avoid processing redundant info that could fail
$start = strpos( $str, '<ResourceList' );
$end = strpos( $str, '</ResourceList>'  );
$str = substr( $str, $start, $end-$start+15 );

$xml = new SimpleXMLElement( $str, LIBXML_NOERROR | LIBXML_NOWARNING  );
print_r( $xml );
// VICTORY

?>

 

Thanks a lot. That has worked.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.