Been struggling all day trying to figure out a way to parse this xml with php:
$xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:recherchePointChronopostResponse xmlns:ns1="http://cxf.rechercheBt.soap.chronopost.fr/">
<return>
<errorCode>0</errorCode>
<errorMessage>Code retour OK</errorMessage>
<listePointRelais>
<accesPersonneMobiliteReduite>true</accesPersonneMobiliteReduite>
<actif>true</actif>
<adresse1>4 RUE DE MONTESSUY</adresse1>
<adresse2/>
<adresse3/>
<codePays>FR</codePays>
<codePostal>75007</codePostal>
<coordGeolocalisationLatitude>48.8597222222</coordGeolocalisationLatitude>
<coordGeolocalisationLongitude>2.304166666670</coordGeolocalisationLongitude>
<distanceEnMetre>852</distanceEnMetre>
<identifiant>2102R</identifiant>
<indiceDeLocalisation/>
<listeHoraireOuverture>
<horairesAsString>08:00-12:00 12:00-20:00</horairesAsString>
<jour>7</jour>
<listeHoraireOuverture>
<debut>08:00</debut>
<fin>12:00</fin>
</listeHoraireOuverture>
<listeHoraireOuverture>
<debut>12:00</debut>
<fin>20:00</fin>
</listeHoraireOuverture>
</listeHoraireOuverture>
</listePointRelais>
<qualiteReponse>2</qualiteReponse>
<wsRequestId/>
</return>
</ns1:recherchePointChronopostResponse>
</soap:Body>
</soap:Envelope>';
and here is the php i use:
$soap = simplexml_load_string($xml);
$soap->registerXPathNamespace('soap', 'http://cxf.rechercheBt.soap.chronopost.fr/');
foreach ($soap->xpath('//ns1:errorMessage') as $val) {
echo $val . "<br>";
}
But i keep having a blank page!! do you please know what is wrong?
I also tried to follow this post converting SOAP XML response to a PHP object or array, in vain!
Thanks
Give this a shot...
$xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:recherchePointChronopostResponse xmlns:ns1="http://cxf.rechercheBt.soap.chronopost.fr/">
<return>
<errorCode>0</errorCode>
<errorMessage>Code retour OK</errorMessage>
<listePointRelais>
<accesPersonneMobiliteReduite>true</accesPersonneMobiliteReduite>
<actif>true</actif>
<adresse1>4 RUE DE MONTESSUY</adresse1>
<adresse2/>
<adresse3/>
<codePays>FR</codePays>
<codePostal>75007</codePostal>
<coordGeolocalisationLatitude>48.8597222222</coordGeolocalisationLatitude>
<coordGeolocalisationLongitude>2.304166666670</coordGeolocalisationLongitude>
<distanceEnMetre>852</distanceEnMetre>
<identifiant>2102R</identifiant>
<indiceDeLocalisation/>
<listeHoraireOuverture>
<horairesAsString>08:00-12:00 12:00-20:00</horairesAsString>
<jour>7</jour>
<listeHoraireOuverture>
<debut>08:00</debut>
<fin>12:00</fin>
</listeHoraireOuverture>
<listeHoraireOuverture>
<debut>12:00</debut>
<fin>20:00</fin>
</listeHoraireOuverture>
</listeHoraireOuverture>
</listePointRelais>
<qualiteReponse>2</qualiteReponse>
<wsRequestId/>
</return>
</ns1:recherchePointChronopostResponse>
</soap:Body>
</soap:Envelope>';
$doc = new DOMDocument();
$doc->loadXML( $xml );
$results = $doc->getElementsByTagName( "errorMessage" );
$errorMessage = $results->item(0)->nodeValue;
echo $errorMessage;
Related
I have this return from a SOAP webservice :
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:historyResponse xmlns:ns2="XXXXXXXXXX">
<return>
<userFullname>AGENT</userFullname>
<date>2018-08-01T17:24:20.948+02:00</date>
<stateName>Préparé</stateName>
</return>
<return>
<userFullname>PARAPHEUR</userFullname>
<date>2018-08-01T17:24:21.039+02:00</date>
<stateName>Envoyé pour signature</stateName>
</return>
<return>
<userFullname>Administrator</userFullname>
<date>2018-08-01T17:24:21.228+02:00</date>
<stateName>Signé</stateName>
</return>
<return>
<userFullname>PARAPHEUR</userFullname>
<date>2018-08-01T17:24:21.324+02:00</date>
<stateName>Classé</stateName>
</return>
</ns2:historyResponse>
</soap:Body>
</soap:Envelope>
When I consume this webservice through PHP and CURL I just get a string like this (var dump) :
string(728) "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:historyResponse xmlns:ns2="http://sei.ws.fast.cdc.com/"><return><userFullname>AGENT EDISSYUM</userFullname><date>2018-08-01T17:24:20.948+02:00</date><stateName>Préparé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.039+02:00</date><stateName>Envoyé pour signature</stateName></return><return><userFullname>Administrator </userFullname><date>2018-08-01T17:24:21.228+02:00</date><stateName>Signé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.324+02:00</date><stateName>Classé</stateName></return></ns2:historyResponse></soap:Body></soap:Envelope>"
How could I have from this string an array of "return" ?
You can use simplexml_load_string() and json functions to convert it to an array. (note that you need to replace the colons as this function doesn't recognize xml with namespaces for some reason)
$response = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:historyResponse xmlns:ns2="http://sei.ws.fast.cdc.com/"><return><userFullname>AGENT EDISSYUM</userFullname><date>2018-08-01T17:24:20.948+02:00</date><stateName>Préparé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.039+02:00</date><stateName>Envoyé pour signature</stateName></return><return><userFullname>Administrator </userFullname><date>2018-08-01T17:24:21.228+02:00</date><stateName>Signé</stateName></return><return><userFullname>PARAPHEUR </userFullname><date>2018-08-01T17:24:21.324+02:00</date><stateName>Classé</stateName></return></ns2:historyResponse></soap:Body></soap:Envelope>';
//I'm not the author of this regex ;)
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$responseArray = json_decode($json,true);
echo '<pre>';
print_r($responseArray);
Hope it helps!! Feel free to ask if you have any doubts :)
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;
?>
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);
?>
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).
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"?>
Here is my XML response from an API:
<?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>
<GetCertificateResponse xmlns="http://url.com">
<GetCertificateResult>
<ReturnValue xmlns="">
<Status>Success</Status>
<Message/>
<CertificateNumber/>
<URL/>
</ReturnValue>
</GetCertificateResult>
</GetCertificateResponse>
</soap:Body>
</soap:Envelope>
How can I reaq the status node? I've tried so many combos:
$getCertificateXMLResponse = simplexml_load_string($getCertificateXMLResponse);
echo $getCertificateXMLResponse->GetCertificateResponse->GetCertificateResult->ReturnValue->Status;
You can also do it with Xpath
$xml = simplexml_load_string($soap, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$result = $xml->xpath('//Status');
echo $result[0];
it's... ugly.. but works
$xml = new SimpleXMLElement(file_get_contents('a.xml'),0,false,'');
$namespaces = $xml->getDocNamespaces(true);
$xml->registerXPathNamespace('empty', $namespaces['']);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$status = array_shift($xml->xpath("soap:Body/empty:GetCertificateResponse/empty:GetCertificateResult/ReturnValue/Status"));
echo $status->asXML();
i'm curious to see more elegant solution.
and indeed take a look at things like nusoap.
How do I read the error_code from this SOAP reply Envelope? My PHP version is: 5.2.0.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<Response xmlns="http://xxx.gateway.xxx.abcd.com">
<return>
<transaction_id>1234567</transaction_id>
<error_code>109</error_code>
</return>
</Response>
</soap:Body>
</soap:Envelope>
I just need to read the value of error_code tag.here the value is :109
I am using nusoap. I used the below code but not working properly:
$response=htmlspecialchars($client->response, ENT_QUOTES);
$xml = simplexml_load_string($response);
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap']);
$error_code = $soap->body->children($ns['error_code']);
<?php
$string = <<<XML
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<Response xmlns="http://xxx.gateway.xxx.abcd.com">
<return>
<transaction_id>1234567</transaction_id>
<error_code>109</error_code>
</return>
</Response>
</soap:Body>
</soap:Envelope>
XML;
$xml = new SimpleXMLElement($string);
$xml->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
$body = $xml->xpath("//soap:Body");
$error_code = (string)$body[0]->Response->return->error_code;
print_r($error_code);
?>
OR
$xml = simplexml_load_string($string);
$error_code = (string)$xml->children('soap', true)
->Body
->children()
->Response
->return
->error_code;
I have just started learning to program with PHP SOAPclient. I have
the following xml file and I want to extract/get the value
rs-1304338811289-11595:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverMDRecordsResponse xmlns:ns2="http://mdstore.data.dnetlib.eu/" xmlns:ns3="http://www.w3.org/2005/08/addressing">
<return>
<ns3:Address>http://129.70.212.20:8282/dnet-mdstore/service/MDStoreResultSet</ns3:Address>
<ns3:ReferenceParameters>
<ResourceIdentifier:ResourceIdentifierxmlns:ResourceIdentifier="http://www.driver.org" xmlns:wsa="http://www.w3.org/2005/08/addressing">rs-1304338811289-11595</ResourceIdentifier:ResourceIdentifier>
</ns3:ReferenceParameters>
-......
</return>
</ns2:deliverMDRecordsResponse>
</soap:Body>
</soap:Envelope>
<?php
$source = 'resourceIdentifier.xml';
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('identifier', 'ns3');
foreach ($xml->xpath('//identifier:ResourceIdentifier') as $item)
{
print_r($item);
}
?>
Your simplexml namespace registration statement is broken:
Try this:
<?
$source=<<<END
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverMDRecordsResponse xmlns:ns2="http://mdstore.data.dnetlib.eu/" xmlns:ns3="http://www.w3.org/2005/08/addressing">
<return>
<ns3:Address>http://129.70.212.20:8282/dnet-mdstore/service/MDStoreResultSet</ns3:Address>
<ns3:ReferenceParameters>
<ResourceIdentifier:ResourceIdentifier xmlns:ResourceIdentifier="http://www.driver.org" xmlns:wsa="http://www.w3.org/2005/08/addressing">r\
s-1304338811289-11595</ResourceIdentifier:ResourceIdentifier>
</ns3:ReferenceParameters>
</return>
</ns2:deliverMDRecordsResponse>
</soap:Body>
</soap:Envelope>
END;
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('t', 'http://www.driver.org');
foreach ($xml->xpath('//t:ResourceIdentifier') as $item)
{
// print_r($item);
echo $item->asXML();
}
?>