1

I am trying to parse the below XML , i have tryed loads of different solutions, i have provided an example of what i have tryed. I have read the SimpleXML documents and i still cant get this right. In the Example below all im trying to do is Echo out a line in the XML.

<?php
$xmlstr = '
<?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>
<SubmitLeadResponse xmlns="https://test.com/">
<SubmitLeadResult>
<Result>C</Result>
<RedirectURL>https://testred.com</RedirectURL>
<ApplicantID>123</ApplicantID>
<ConfirmedPrice>0</ConfirmedPrice>
<PotentialPrice>0</PotentialPrice>
</SubmitLeadResult>
</SubmitLeadResponse>
</soap:Body>
</soap:Envelope>'
;

?>


<?php

$SubmitLeadResponse = new SimpleXMLElement($xmlstr);
echo $SubmitLeadResponse->SubmitLeadResult[0]->RedirectURL;

?>
Markus
  • 2,071
  • 4
  • 22
  • 44

3 Answers3

1

You can try below code for SimpleXML

<?php
$xml ='<?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>
<SubmitLeadResponse xmlns="https://test.com/">
<SubmitLeadResult>
<Result>C</Result>
<RedirectURL>https://testred.com</RedirectURL>
<ApplicantID>123</ApplicantID>
<ConfirmedPrice>0</ConfirmedPrice>
<PotentialPrice>0</PotentialPrice>
</SubmitLeadResult>
</SubmitLeadResponse>
</soap:Body>
</soap:Envelope>';
$get_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $xml);
$xml = simplexml_load_string($get_xml);
print"<pre>";
print_r((string)$xml->Body->SubmitLeadResponse->SubmitLeadResult->RedirectURL);
echo "<br /><br /><br />";
print_r($xml);
?>
Sanjeev Chauhan
  • 3,977
  • 3
  • 24
  • 30
  • Hi @Chauhan , thank you. Could you add the your example , IF the RedirectURL contains a URL it gets Redirected to www.test.com and if it does not then echo "Failed" – xTrifactorx Jun 17 '15 at 10:29
  • $getRedirectUrl = (string)$xml->Body->SubmitLeadResponse->SubmitLeadResult->RedirectURL; if($getRedirectUrl) { header("location:".$getRedirectUrl); } else { echo "Failed"; } Add this line after $xml = simplexml_load_string($get_xml); – Sanjeev Chauhan Jun 17 '15 at 10:42
  • Thank you so much , if i can ask you one last question please i promise. How do i : IF Result = C then redirect using the redirect URL , IF Result is any other value echo Failed. Thank you so much :D – xTrifactorx Jun 17 '15 at 11:53
  • I am not getting you for Result = C? – Sanjeev Chauhan Jun 17 '15 at 11:54
  • In the XML i get a C . If i get a C then they provide a URL , if i get any other result then they provide a failed url – xTrifactorx Jun 17 '15 at 12:08
  • $getResult = (string)$xml->Body->SubmitLeadResponse->SubmitLeadResult->Result; $getRedirectUrl = (string)$xml->Body->SubmitLeadResponse->SubmitLeadResult->RedirectURL; if($getResult=='C') { header("location:".$getRedirectUrl); } else { echo "Failed"; } – Sanjeev Chauhan Jun 17 '15 at 12:15
0

I've changed your code a bit. Here's a working sample to get the RedirectURL:

<?php
$xmlstr = '<?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>
<SubmitLeadResponse xmlns="https://test.com/">
<SubmitLeadResult>
<Result>C</Result>
<RedirectURL>https://testred.com</RedirectURL>
<ApplicantID>123</ApplicantID>
<ConfirmedPrice>0</ConfirmedPrice>
<PotentialPrice>0</PotentialPrice>
</SubmitLeadResult>
</SubmitLeadResponse>
</soap:Body>
</soap:Envelope>';

$doc = new DOMDocument();
$doc->loadXML( $xmlstr );

$RedirectURL = $doc->getElementsByTagName( "RedirectURL" );
$RedirectURL = $LoginResults->item(0)->nodeValue;

var_dump( $RedirectURL );

Sample provided from this source: There are also more informations to reading SOAP-envelopes without a soapclient

Please note that it's good practice to omit the php closing tag and stay in the php-context as long as possible to avoid unexpected outputs (linebreaks).

Community
  • 1
  • 1
berkyl
  • 431
  • 4
  • 20
0

Your XML contains namespaced elements, so it's a little more complicated to parse. It can be done by passing the namespace values to children() like so:

Codepad demo

$SubmitLeadResponse = new SimpleXMLElement($xmlstr);
echo (string)$SubmitLeadResponse
          ->children('http://schemas.xmlsoap.org/soap/envelope/')
          ->Body
          ->children('https://test.com/')
          ->SubmitLeadResponse
          ->SubmitLeadResult
          ->RedirectURL;

Outputs

https://testred.com

Note: SimpleXML doesn't like new lines before the XML string, so remove the new line, making it:

$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
MrCode
  • 63,975
  • 10
  • 90
  • 112