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; ?> 

3 Answers 3

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); ?> 
Sign up to request clarification or add additional context in comments.

6 Comments

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"
$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);
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
I am not getting you for Result = C?
In the XML i get a <Result>C</Result> . If i get a <Result>C</Result> then they provide a URL , if i get any other result then they provide a failed url
|
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).

Comments

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"?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.