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();
}
?>
Related
I get different XML strings via SOAP.
But it is very difficult for me to get the value from XML with PHP.
XML examples:
<?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>
<GetUserInfoResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
<GetUserInfoResult>
<GetUserInfo>
<User ID="23" />
</GetUserInfo>
</GetUserInfoResult>
</GetUserInfoResponse>
</soap:Body>
</soap:Envelope>
<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<rs:data>
<z:row ows_ID="128" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
I would like to get the id.
I tried it like this:
$xml_element = simplexml_load_string($responseContent);
$name_spaces = $xml_element->getNamespaces(true);
$soap = $xml_element->children($name_spaces['soap'])
->Body
->children($name_spaces['rs'])
->GetListItemsResponse
->GetListItemsResult
->listitems
->{'rs:data'}
->{'z:row'}['ows_ID'][0];
But most time I dont know how to get my value.
Is it possible to display a whole array or how do I get the path to the value?
What you could do to get the value of 'ows_ID' is to use the SimpleXMLElement children method and also add the namespaces for the child elements.
You can use the attributes method to get the value for 'ows_ID';
For example:
$responseContent = <<<XML
<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<rs:data>
<z:row ows_ID="128" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
XML;
$xml_element = simplexml_load_string($responseContent);
$name_spaces = $xml_element->getNamespaces(true);
$rows = $xml_element
->children($name_spaces['soap'])
->Body
->children()
->GetListItemsResponse
->GetListItemsResult
->listitems
->children($name_spaces['rs'])
->children($name_spaces['z']);
foreach ($rows as $row) {
$ows_ID = $row->attributes()->ows_ID;
}
Demo
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"?>
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;
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;