How can I parse a XML response to PHP? I have tried several solutions but nothing works. Here is the XML I get back:
<soapenv:envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:body>
<ns:getrateresponse xmlns:ns="http://services.gts">
<ns:return xmlns:ax25="http://services.gts/xsd" xmlns:ax26="http://model.gts/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax25:RaterResponseAutoQuotes">
<ax25:carriers xsi:type="ax25:RaterResponseCarriersAutoQuotes">
<ax25:accessorials xsi:type="ax25:RaterResponseAccessorial">
<ax25:aramount>12.66</ax25:aramount>
<ax25:accessorialid>22</ax25:accessorialid>
<ax25:accessorialname>Fuel</ax25:accessorialname>
</ax25:accessorials>
<ax25:ar_final_rate>161.66</ax25:ar_final_rate>
<ax25:carrier_id>0000087</ax25:carrier_id>
<ax25:carrier_name>CON-WAY FREIGHT INC</ax25:carrier_name>
<ax25:service_days>04</ax25:service_days>
</ax25:carriers>
<ax25:message>Success</ax25:message>
<ax25:referencenumber>3184877</ax25:referencenumber>
<ax25:success>true</ax25:success>
</ns:return>
</ns:getrateresponse>
</soapenv:body>
Parse:
foreach($xml->ax25:carriers as $carrier) {
$$carrierObject = array(
"rate" => $carrier->ar_final_rate,
);
array_push($carriers, $$carrierObject);
}
All I care about is the each ax25:carriers ax25:ar_final_rate. I also tried
$result = new SimpleXMLElement($response);
but get back
object(SimpleXMLElement)#2 (0) { }
Here we are using DOMDocument for extracting textContent inside these tags <ax25:ar_final_rate>
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string='<soapenv:envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:body>
<ns:getrateresponse xmlns:ns="http://services.gts">
<ns:return xmlns:ax25="http://services.gts/xsd" xmlns:ax26="http://model.gts/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax25:RaterResponseAutoQuotes">
<ax25:carriers xsi:type="ax25:RaterResponseCarriersAutoQuotes">
<ax25:accessorials xsi:type="ax25:RaterResponseAccessorial">
<ax25:aramount>12.66</ax25:aramount>
<ax25:accessorialid>22</ax25:accessorialid>
<ax25:accessorialname>Fuel</ax25:accessorialname>
</ax25:accessorials>
<ax25:ar_final_rate>161.66</ax25:ar_final_rate>
<ax25:carrier_id>0000087</ax25:carrier_id>
<ax25:carrier_name>CON-WAY FREIGHT INC</ax25:carrier_name>
<ax25:service_days>04</ax25:service_days>
</ax25:carriers>
<ax25:message>Success</ax25:message>
<ax25:referencenumber>3184877</ax25:referencenumber>
<ax25:success>true</ax25:success>
</ns:return>
</ns:getrateresponse>
</soapenv:body>
</soapenv:envelope>';
$domDocument = new DOMDocument();
$domDocument->loadXML($string);
$carriers=array();
$results=$domDocument->getElementsByTagNameNS("http://services.gts/xsd", "ar_final_rate");
foreach($results as $result)
{
array_push($carriers, $result->textContent);
}
print_r($carriers);
Output:
Array
(
[0] => 161.66
)
Related
I have XML document:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<saml2p:ArtifactResolve xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Version="2.0"
IssueInstant="2020-06-01T10:25:15+02:00">
<saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">TEST</saml2:Issuer>
<saml2p:Artifact>AAQAAKFbFR94fxqmioAqjJUwfHTFVHTDBTVHdBwwTW+ehcM19zsk=</saml2p:Artifact>
</saml2p:ArtifactResolve>
</soapenv:Body>
</soapenv:Envelope>
I try to do this in this way:
$results = array();
$filename = 'cert.p12';
$password = 'certpass';
$priv_key = openssl_pkcs12_read(file_get_contents($filename), $results, $password);
$doc = new DOMDocument();
$doc->loadXML($xml);
$xp = new DOMXPath($doc);
$xp->registerNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
$xp->registerNamespace('saml2p','urn:oasis:names:tc:SAML:2.0:protocol');
$xp->registerNamespace('saml2','urn:oasis:names:tc:SAML:2.0:assertion');
$xp->registerNamespace('ds',XMLSecurityDSig::XMLDSIGNS);
$artifactResolveNode = $xp->query('/*[local-name()=\'Envelope\']/*[local-name()=\'Body\']/*[local-name()=\'ArtifactResolve\']')->item(0);
if($artifactResolveNode){
$objDSig = new XMLSecurityDSig();
$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
$objDSig->addReference(
$artifactResolveNode,
XMLSecurityDSig::SHA256,
array('http://www.w3.org/2000/09/xmldsig#enveloped-signature', 'http://www.w3.org/2001/10/xml-exc-c14n#'),
array('force_uri' => true)
);
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'private'));
$objKey->loadKey($results['pkey'], FALSE);
$objDSig->sign($objKey);
$objDSig->add509Cert($results['cert']);
$objDSig->appendSignature($doc->documentElement());
echo $doc->saveXML();
}
Output with Singnature in wrong location is:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<saml2p:ArtifactResolve xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Version="2.0" IssueInstant="2020-06-01T10:25:15+02:00" Id="pfx97783ab1-0339-0f2e-b759-9e9c07e347b0">
<saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">TEST</saml2:Issuer><saml2p:Artifact>AAQAAKFbFR94fxqmioAqjJUwfyUtjJbv0uEPB7ooopodBwwTW+ehcM19zsk=</saml2p:Artifact></saml2p:ArtifactResolve>
</soapenv:Body>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
(...)
</soapenv:Envelope>
I want to put Signature node between ISSUER and ARTIFACT. This location is very important to correct send SOAP Envelope.
It is possible?
I found!
I need add code below:
$artifactNode = $xp->query('/*[local-name()=\'Envelope\']/*[local-name()=\'Body\']/*[local-name()=\'ArtifactResolve\']/*[local-name()=\'Artifact\']')->item(0);
and append Signature like this:
$objDSig->insertSignature($artifactResolveNode,$artifactNode);
I would like to convert the SOAP XML response into the JSON format. Can anybody please help!
I got below SOAP XML in $response
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:awsse="http://xml.amadeus.com/2010/06/Session_v3" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<soapenv:Header>
<wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
<wsa:From>
<wsa:Address>https://nodeD2.test.webservices.amadeus.com/XXXXXXXX</wsa:Address>
</wsa:From>
<wsa:Action>http://webservices.amadeus.com/FMPTBQ_18_1_1A
</wsa:Action>
<wsa:MessageID>urn:uuid:a4844bc6-2e05-e8e4-b589-6f813ecad262</wsa:MessageID>
<wsa:RelatesTo RelationshipType="http://www.w3.org/2005/08/addressing/reply">urn:uuid:ac1c8436-ecaf-4bbb-a9ff-11b466dffe18</wsa:RelatesTo>
<awsse:Session TransactionStatusCode="End"><awsse:SessionId>00896YTRZQPQ</awsse:SessionId><awsse:SequenceNumber>1</awsse:SequenceNumber><awsse:SecurityToken>TRFTGYNBHUYKIOL</awsse:SecurityToken></awsse:Session>
</soapenv:Header>
<soapenv:Body>
<Fare_MasterPricerTravelBoardSearchReply xmlns="http://xml.amadeus.com/FMPTBR_18_1_1A">
<replyStatus><status><advisoryTypeInfo>FQX</advisoryTypeInfo></status></replyStatus>
<conversionRate><conversionRateDetail><currency>USD</currency></conversionRateDetail></conversionRate>
<familyInformation><refNumber>1</refNumber><fareFamilyname>BAGSEAT</fareFamilyname><description>OPTIMA</description><carrier>IB</carrier><services><reference>1</reference><status>CHA</status></services><services><reference>2</reference><status>CHA</status></services><services><reference>3</reference><status>INC</status></services><services><reference>4</reference><status>NOF</status></services><services><reference>5</reference><status>NOF</status></services><services><reference>6</reference><status>NOF</status></services><services><reference>7</reference><status>NOF</status></services></familyInformation>
</Fare_MasterPricerTravelBoardSearchReply>
</soapenv:Body>
</soapenv:Envelope>
I tried this.
$xml = json_decode(json_encode(simplexml_load_string($response)), true);
print '<pre>';
print_r($xml);
print '</pre>';
I got the solution to the problem, working fine
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$response = simplexml_load_string($response);
$dataArray = json_decode(json_encode((array)$response), TRUE);
echo '<pre>';print_r($dataArray);echo '</pre>';die(__FILE__.' On this line '.__LINE__);
Hye
I'm trying to use a WebService with SOAP in PHP. But the SoapVar creates my DOM XML with double XML tag
My code :
$xmlContent = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:com="http://schemas.datacontract.org/2004/07/Commun.Metier">
<soapenv:Header/>
<soapenv:Body>
<tem:SendData> ....';
$soapClient = new SoapClient("URL OF THE WSDL", array('trace' => 1, 'location' => 'URL OF THE SERVICE', 'uri' => 'URL OF THE SERVICE'));
$myXML = new SoapVar($xmlContent, XSD_ANYXML);
try {
$result = $soapClient->__soapCall("SendData", array('SendData' => $myXML));
} catch (Exception $e) {
var_dump($e);
}
The problem is that I have an exception Bad request because the XML I'm sending haves 2 XML tags and Soap Enveloppe :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body><?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:com="http://schemas.datacontract.org/2004/07/Commun.Metier">
<soapenv:Header/>
<soapenv:Body>
<tem:SendData>
How can I cancel this double tag ?
Thanks
I just started with web service using SOAP.
I using below code to get response.
$response = $objClient->__getLastResponse();
My response is:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getHotelValuedAvail xsi:type="xsd:string" xmlns:ns1="http://axis.frontend.hydra.hotelbeds.com">
<HotelValuedAvailRS xmlns="http://www.hotelbeds.com/schemas/2005/06/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" timeToExpiration="1771163" totalItems="387" echoToken="DummyEchoToken">
<PaginationData currentPage="1" totalPages="387"/>
<ServiceHotel xsi:type="ServiceHotel" availToken="ngQXOz+UOoLDtPieFMhu9wdw">
<DateFrom date="20130709"/>
<DateTo date="20130711"/>
<Currency code="EUR">Euro</Currency>
<HotelInfo xsi:type="ProductHotel">
<Code>99361</Code>
<Name>Hilton Sa Torre Mallorca Resort</Name>
<Position latitude="39.42761529999999936535" longitude="2.75602390000000019299"/>
</HotelInfo>
</ServiceHotel>
</HotelValuedAvailRS>
</ns1:getHotelValuedAvail>
</soapenv:Body>
</soapenv:Envelope>
Can some one help me to get the value of Name Element?
I have tried simplexml_load_string($response) and and X-Path("/Name"), but i could not get it.
You can try using DOMDocument::getElementsByTagName :
You can try like this:
$response = $objClient->__getLastResponse();
$doc = new DOMDocument();
$doc->loadXML($response);
$names= $doc->getElementsByTagName( "Name" );
$name = $names->item(0)->nodeValue;
var_export( $name );
I've tried several examples on here so far but just can't seem to figure out how to process this soap response with PHP 5.3:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getAuthenticationTokenResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://earthquake">
<getAuthenticationTokenReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">1CFA9FF89E1F6E2AC62C4E5A689EED65F144B7E827C386A60836D97A76E0C49F47FCC94637AFADB8EAEDF3110955F2622AB334B92EBFC568E563662C2202E51157A4D9AABBFDD9941119CC8C96681B51EE453006C460B50C6104A5E07C84CE88</getAuthenticationTokenReturn>
</ns1:getAuthenticationTokenResponse>
</soapenv:Body>
</soapenv:Envelope>
I'm trying to get the getAuthenticationTokenReturn into a varaible but have not had much luck so far.
Here is my code so far:
$xml = simplexml_load_string($result);
$xml->registerXPathNamespace('ns1','http://schemas.xmlsoap.org/soap/encoding/');
foreach ($xml->xpath('//ns1:getAuthenticationTokenResponse') as $item)
{
echo 'Name: '.$item,'<br>';
}
Try this, i have tried and i got the token
$string = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getAuthenticationTokenResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://earthquake">
<getAuthenticationTokenReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">1CFA9FF89E1F6E2AC62C4E5A689EED65F144B7E827C386A60836D97A76E0C49F47FCC94637AFADB8EAEDF3110955F2622AB334B92EBFC568E563662C2202E51157A4D9AABBFDD9941119CC8C96681B51EE453006C460B50C6104A5E07C84CE88</getAuthenticationTokenReturn>
</ns1:getAuthenticationTokenResponse>
</soapenv:Body>
</soapenv:Envelope>';
$xml = simplexml_load_string($string);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('ns1', 'http://earthquake');
foreach ($xml->xpath('//ns1:getAuthenticationTokenResponse') as $item)
{
echo $item->asXML();
}