Neculai Andrei
Neculai Andrei

Reputation: 223

How to parse soap xml response in php and get the information from the string

i have some problem extracting the information from a SOAP response. This is hte response I get:

<?xml version="1.0" encoding="utf-8" ?> 
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <soap:Body>
- <GetInfoFromSendingResponse xmlns="http://test.test.com/">
  <GetInfoFromSendingResult>{"SendingID":"2468","Subject":"Test","ID":"2468","CampaignID":"890","ForwardAddress":"[email protected]","SendingTime":"1/14/2016 8:00:00 AM","SendLeadsToEmail":"0","LanguageID":"6","LeadsTestMode":true,"WebversionLink":"","Language":"FR"}</GetInfoFromSendingResult> 
  </GetInfoFromSendingResponse>
  </soap:Body>
  </soap:Envelope>

I need the information from GetInfoFromSendingResult and store that in a variable so I can then use that information.

Example: Change the language of a form based on the "Language" info provided in the SOAP response. Any help is appreciated.

Upvotes: 5

Views: 7921

Answers (2)

The fourth bird
The fourth bird

Reputation: 163577

Another possible solution is to use for example SimpleXML. You can register the namespace and use an xpath expression:

$source = <<<SOURCE
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetInfoFromSendingResponse xmlns="http://test.test.com/">
            <GetInfoFromSendingResult>{"SendingID":"2468","Subject":"Test","ID":"2468","CampaignID":"890","ForwardAddress":"[email protected]","SendingTime":"1/14/2016 8:00:00 AM","SendLeadsToEmail":"0","LanguageID":"6","LeadsTestMode":true,"WebversionLink":"","Language":"FR"}</GetInfoFromSendingResult>
        </GetInfoFromSendingResponse>
    </soap:Body>
</soap:Envelope>
SOURCE;

$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('test', 'http://test.test.com/');
$elements = $xml->xpath('//soap:Envelope/soap:Body/test:GetInfoFromSendingResponse/test:GetInfoFromSendingResult');
$result = json_decode($elements[0], true);
print_r($result);

Will result in:

Array
(
    [SendingID] => 2468
    [Subject] => Test
    [ID] => 2468
    [CampaignID] => 890
    [ForwardAddress] => [email protected]
    [SendingTime] => 1/14/2016 8:00:00 AM
    [SendLeadsToEmail] => 0
    [LanguageID] => 6
    [LeadsTestMode] => 1
    [WebversionLink] => 
    [Language] => FR
)

Upvotes: 4

İrfan Erol
İrfan Erol

Reputation: 21

You can use SoapClient comes with PHP 5.0+ versions

$client = new SoapClient("http://test.test.com/?wsdl");
$res = $client->SoapFunction(array('param1'=>'value','param2'=>'value'));
echo $res->GetInfoFromSendingResponse->GetInfoFromSendingResult;

Then you might need JSON decoding to get some specific value in it.

Upvotes: 2

Related Questions