1

I don’t have much experience with SOAP but am required to build a validator for my work and using a specific SOAP wsdl.

I’ve set up the connection and call and everything works fine but I receive the response data in what I assume is XML, here is a small portion of the output:

<user id=”54354334543”>
<firstname>My firstname</firstname>
<lastname>My lastname</lastname>
<email>[email protected]</email>
<nickname>My Nickname</nickname>
<source>Update_and_add</source>
</user>

This response is one step in the validation so I’ve to be able to retrieve single items such as and and use them for further look-ups.

So my question is, how do I parse this in order to get the items out? I’ve tried different approaches and convert it into an array but I haven’t had any luck doing so.

Any suggestions or help would be very much appreciated.

Regards,
- Mestika

3 Answers 3

1
$xml = '<user id="54354334543">
<firstname>My firstname</firstname>
<lastname>My lastname</lastname>
<email>[email protected]</email>
<nickname>My Nickname</nickname>
<source>Update_and_add</source>
</user>';
print_r(json_decode(json_encode(simplexml_load_string($xml))));

output:

stdClass Object
(
    [@attributes] => stdClass Object
        (
            [id] => 54354334543
        )

    [firstname] => My firstname
    [lastname] => My lastname
    [email] => [email protected]
    [nickname] => My Nickname
    [source] => Update_and_add
)

So you could just do something like:

$data = json_decode(json_encode(simplexml_load_string($xml)));
echo $data->nickname;
Sign up to request clarification or add additional context in comments.

Comments

0

There's also this function:

http://php.net/manual/en/book.simplexml.php#108035

Which would probably be more performant and clearer, though I'd try the filtering through JSON solution first.

Comments

0

Try this

<?php
$k = '<user id="54354334543">
<firstname>My firstname</firstname>
<lastname>My lastname</lastname>
<email>[email protected]</email>
<nickname>My Nickname</nickname>
<source>Update_and_add</source>
</user>';
$xml = simplexml_load_string($k);
echo $id =  $xml['id']; 
echo "<br>".$firstname =$xml->firstname;
?>

Output

54354334543
My firstname

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.