3

I need to access the response from a .NET web service which returns data in XML format. How can I split the data returned? For example, I would like to parse the data into some PHP variables:

$name = "Dupont";
$email = "[email protected]";

I have been looking for how to do this for a long time without finding the correct way to do it.

My script is:

$result = $client->StudentGetInformation($params_4)->StudentGetInformationResult;

    echo "<p><pre>" . print_r($result, true) . "</pre></p>";

The echo in my page is:

stdClass Object
(
    [any] => 0Successful10371DupontCharlescharles.dupont@[email protected] FINANCE1778AAA Département
)

The webservice response format is:

<?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>
    <StudentGetInformationResponse xmlns="http://tempuri.org/">
      <StudentGetInformationResult>xml</StudentGetInformationResult>
    </StudentGetInformationResponse>
  </soap:Body>
</soap:Envelope>

I have tried your example. But it doesn't do what i need. I need to split the value returned. I would like to get the data and put them into PHP variables:

$name = "Dupont"; $email = "[email protected]"; etc...

Unfortunately the echo of your example gives:

object(stdClass)#1 (1) { ["StudentGetInformationResult"]=> object(stdClass)#11 (1) { ["any"]=> string(561) "0Successful10371DupontCharlescharles.dupont@[email protected] FINANCE1778AAA Département" } } 
2
  • SoapClient php.net/manual/en/class.soapclient.php works like charm Commented Apr 6, 2012 at 13:12
  • Thanks... but do you know which class do i have to use? Commented Apr 6, 2012 at 13:34

1 Answer 1

1

The only class you need is SoapClient. There are a lot of examples you can use in the PHP Documentation.

Example:

try {
    $client = new SoapClient ( "some.aspx?wsdl" );
    $result = $client->StudentGetInformation ( $params_4 );

    $xml = simplexml_load_string($result->StudentGetInformationResult->any);
    echo "<pre>" ;

    foreach ($xml as $key => $value)
    {
        foreach($value as $ekey => $eValue)
        {
            print($ekey . " = " . $eValue . PHP_EOL);
        }
    }

} catch ( SoapFault $fault ) {
    trigger_error ( "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR );
}

Output

Code = 0
Message = Successful
stud_id = 10373
lname = Dupont
fname = Charles
loginid = [email protected]
password = 1234
email = [email protected]
extid = 
fdisable = false
culture = fr-FR
comp_id = 1003
comp_name = FIRST FINANCE
dept_id = 1778
dept_name = Certification CMF (Test web service)
udtf1 = 
udtf2 = 
udtf3 = 
udtf4 = 
udtf5 = 
udtf6 = 
udtf7 = 
udtf8 = 
udtf9 = 
udtf10 = 
Audiences = 
Sign up to request clarification or add additional context in comments.

10 Comments

i have to wait 7 hours before to use the Answer button!
I have tried your example. But it doesn't do what i need. I need to split the value returned. I would like to get the data and put them into PHP variables: $name = "Dupont"; $email = "[email protected]"; etc... Unfortunately the echo of your example gives: code object(stdClass)#1 (1) { ["StudentGetInformationResult"]=> object(stdClass)#11 (1) { ["any"]=> string(561) "0Successful10371DupontCharlescharles.dupont@[email protected] FINANCE1778AAA Département" } }code
The values is at $result->StudentGetInformationResult->any and the format you are getting 0Successful10371DupontCharlescharles.dupont@[email protected] FINANCE1778AAA Département lookas a bit strange .. can you put the wasdl link let me try it myself ??? @emb
The echo of $result->StudentGetInformationResult->any gives the string: 0Successful10373DupontCharlescharles.dupont@[email protected] FINANCE1778Certification CMF. I unfortunately cannot give you the wsdl url. I still look for a way to access each part of data given in this string.
I would love if this kind of code would work, but it doesn't...: $result->StudentGetInformationResult->any['email']
|

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.