How do I read SOAP reply Envelope by PHP - php

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;

Related

Get value from json response with SOAP request

After successfully getting a response from a SOAP request in JSON format, I cannot extract one property out of it.
Beholde the response I got from postman.
<?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>
<AutenticacionResponse xmlns="https://figs.software/">
<AutenticacionResult xsi:type="xsd:string">{"CodRespuesta":"00","Respuesta":"bd026f95-61cf-4947-80df-bf519d544995","URL":null,"NCF":null}</AutenticacionResult>
</AutenticacionResponse>
</soap:Body>
</soap:Envelope>
My goad is to get the token of the Respuesta property.
I'm using curl of PHP to establish the connection:
I try to convert the response I got into an array like this:
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//soapBody ')[0];
$array = json_decode(json_encode((array)$body), TRUE);
echo $array['AutenticacionResponse']['AutenticacionResult'];
echo gettype($array);
I have this result:
{"CodRespuesta":"00","Respuesta":"d5810796-9563-4423-aff3-089d61e170b6","URL":null,"NCF":null}
array
How can I get the value of Respuesta ?
Without touching your code I get the answer by just doing
$json = json_decode($array['AutenticacionResponse']['AutenticacionResult'], true);
echo $json['Respuesta'];
And I would have done like this
<?php
$response = <<<XML
<?xml version="1.0"?>
<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>
<AutenticacionResponse xmlns="https://figs.software/">
<AutenticacionResult xsi:type="xsd:string">{"CodRespuesta":"00","Respuesta":"bd026f95-61cf-4947-80df-bf519d544995","URL":null,"NCF":null}</AutenticacionResult>
</AutenticacionResponse>
</soap:Body>
</soap:Envelope>
XML;
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$xml = new SimpleXMLElement($response);
$json = json_decode($xml->soapBody->AutenticacionResponse->AutenticacionResult, true);
echo $json['Respuesta'];

parse the soap xml response to array

I had used curl to call the soap server and i got the response like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GenerateAuthPasswordResponse xmlns="NepalTelecom.AuthGateway">
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
</soap:Body>
</soap:Envelope>
and when i tried to parse the soap xml as:
$response = $this->SoapModel->soapCall($xml , $this->vas_wsdl_url);
$obj = simplexml_load_string($response);
echo $obj;die();
[Note: where $response is the above soap response provided in soap xml]
and i get the $obj as some error like this:
Severity: Warning
Message: simplexml_load_string(): namespace warning : xmlns: URI NepalTelecom.AuthGateway is not absolute
please any body could help so fix this issue.
Thank you in advance.
Try this.
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
</soap:Body>
</soap:Envelope>';
$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap']);
$res = $soap->Body->children();
print_r($res);

PHP XML cant get values

I have a string like this:
<?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>
<rs:data>
<z:row ows_MetaInfo='128;#' />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
I would like to get the value 128.
I already tried simplexml_load_string which is empty.
How can I get this attribute?
Namespace is important in soap response. Try below code:
<?php
$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>
<rs:data>
<z:row ows_MetaInfo=\'128;#\'/>
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>';
$xml_element = simplexml_load_string($xml);
$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_MetaInfo'][0];
echo (string) $soap;
?>

Struggling to read a node with SimpleXML

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 to extract value using php soapclient

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();
}
?>

Categories