Solved

How to retrieve information from soap response?

Posted on 2009-07-14
2
1,463 Views
Last Modified: 2013-11-18
Hi,

I'm trying to make a soap call to an api and use a piece of information from the response I get.
Below is the code I use, the part where I make a soap call works fine, I get a correct response from the server but here comes the problem I have, I can't retrieve the information I need from the response.
The response contains an ID between the response-tags I need for a database insert.

I tried retrieving information with simplexml_load_string but that didn't work, so now I'm trying it with xml_parse_into_struct.
I use this in the code below and I see the information when I use print_r but I can't seem to access it.

Can anybody tell me how to access the information or show me an other method to retrieve this information from the response?

I'm new to PHP so maybe I'm asking a very simple question but I can't seem to get it right or find a straight forward answer to my problem anywhere.
Thanks in advance!
PHP:

<?php

	$client = new SoapClient('https://api.tripolis.com/soap/1.5/DialogueService?wsdl', array('trace' => true, 'exceptions' => false));
 

	//piece of code that creates $params
 

	try

	{

		$client->upsertContact($params);
 

		$response_string = $client->__getLastResponse();
 

		//removes headers from response

		$start = strpos($response_string, '<soap:Envelope');

		$end = strrpos($response_string, '>');

		$response_string = '<?xml version="1.0" encoding="utf-8"?>'. substr($response_string, $start, $end-$start+1);
 

		$simple = $response_string;

		$p = xml_parser_create();

		xml_parse_into_struct($p, $simple, $vals, $index);

		xml_parser_free($p);
 

		echo 'Index array\n';

		print_r($index);

		echo '\nVals array\n';

		print_r($vals);
 

		exit();

	}

	catch (SoapFault $fault)

	{

		echo '<hr>At line number: '.__LINE__.' SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})';

		echo '<hr><pre>';

		var_dump($client->__getLastRequest());

		echo '</pre>';
 

		exit();

	}

?>
 

XML:

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

	<soap:Header>

		<api-version>1.5.9.1</api-version>

	</soap:Header>

	<soap:Body>

		<ns2:upsertContactResponse xmlns:ns2="http://api.tripolis.com/">

			<response>Mjg1Jhe03MjCMdCDprzC_0ZQpPqczsOvM</response>

		</ns2:upsertContactResponse>

	</soap:Body>

</soap:Envelope>
 

xml_parse_into_struct:

Index array

Array

(

    [SOAP:ENVELOPE] => Array

        (

            [0] => 0

            [1] => 9

        )

    [SOAP:HEADER] => Array

        (

            [0] => 1

            [1] => 3

        )

    [API-VERSION] => Array

        (

            [0] => 2

        )

    [SOAP:BODY] => Array

        (

            [0] => 4

            [1] => 8

        )

    [NS2:UPSERTCONTACTRESPONSE] => Array

        (

            [0] => 5

            [1] => 7

        )

    [RESPONSE] => Array

        (

            [0] => 6

        )

)
 

Vals array

Array

(

    [0] => Array

        (

            [tag] => SOAP:ENVELOPE

            [type] => open

            [level] => 1

            [attributes] => Array

                (

                    [XMLNS:SOAP] => http://schemas.xmlsoap.org/soap/envelope/

                )

        )

    [1] => Array

        (

            [tag] => SOAP:HEADER

            [type] => open

            [level] => 2

        )

    [2] => Array

        (

            [tag] => API-VERSION

            [type] => complete

            [level] => 3

            [value] => 1.5.9.1

        )

    [3] => Array

        (

            [tag] => SOAP:HEADER

            [type] => close

            [level] => 2

        )

    [4] => Array

        (

            [tag] => SOAP:BODY

            [type] => open

            [level] => 2

        )

    [5] => Array

        (

            [tag] => NS2:UPSERTCONTACTRESPONSE

            [type] => open

            [level] => 3

            [attributes] => Array

                (

                    [XMLNS:NS2] => http://api.tripolis.com/

                )

        )

    [6] => Array

        (

            [tag] => RESPONSE

            [type] => complete

            [level] => 4

            [value] => Mjg1NjgzMjCMdCDprzC_oZ8rP*czsOVN

        )

    [7] => Array

        (

            [tag] => NS2:UPSERTCONTACTRESPONSE

            [type] => close

            [level] => 3

        )

    [8] => Array

        (

            [tag] => SOAP:BODY

            [type] => close

            [level] => 2

        )

    [9] => Array

        (

            [tag] => SOAP:ENVELOPE

            [type] => close

            [level] => 1

        )

)

Open in new window

0
Comment
Question by:seloh
2 Comments
 
LVL 7

Accepted Solution

by:
ajkhalifa earned 250 total points
ID: 24850929
the response is a tree of  arrays....
so you need to dig your info... and try to fetch...
lets take an example:
if i need to print the tag and the type of each record... then i wl do:
foreach($vals as $val){
    print $val['tag'];
    print "<br>";
    print $val['type'];
    print "<br><br>";
}
0
 
LVL 2

Author Comment

by:seloh
ID: 24891085
Thanks ajkhalifa!

I now loop through each value and depending on keys a assign them to a certain variable via a switch!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Responsive Design Tools 6 38
Time difference 10 35
Editing login page in zencart. 2 14
XML & .net 5 20
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now