How do I get XML data from a web service using PHP?

I need to get a response from a .NET web service that returns data in XML format. How can I share the received data? For example, I would like to parse data into some PHP variables:

$name = "Dupont";
$email = "charles.dupont@societe.com";

      

I've been looking for how to do this for a long time without finding the right way to do it.

My script:

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

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

      

Echo on my page:

stdClass Object
(
    [any] => 0Successful10371DupontCharlescharles.dupont@societe.com1234charles.dupont@societe.comfalsefr-FR1003FIRST FINANCE1778AAA Département
)

      

Web service response format:

<?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 tried your example. But he doesn't do what I need. I need to split the return value. I would like to get data and put it in PHP variables:

$ name = "Dupont"; $ email = " charles.dupont@societe.com "; etc...

Unfortunately, echoing your example gives:

object(stdClass)#1 (1) { ["StudentGetInformationResult"]=> object(stdClass)#11 (1) { ["any"]=> string(561) "0Successful10371DupontCharlescharles.dupont@societe.com1234charles.dupont@societe.comfalsefr-FR1003FIRST FINANCE1778AAA Département" } } 

      

+3


source to share


1 answer


You only need a class SoapClient

. There are many examples that can be used 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 = charles.dupont@societe.com
password = 1234
email = charles.dupont@societe.com
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 = 

      

+1


source







All Articles