Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" } } 
like image 566
emb Avatar asked May 04 '26 10:05

emb


1 Answers

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 = 
like image 187
Baba Avatar answered May 07 '26 01:05

Baba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!