php soap client with nested childs - php

I am trying to figure how to create this soap envelope using soap client
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Header>
<m:sendDataExtraccionTraza
xmlns:m="http://www.mop.cl/controlextraccion/xsd/datosExtraccion/SendDataExtraccionRequest">
<m:codigoDeLaObra>OB-0303-204</m:codigoDeLaObra>
<m:timeStampOrigen>2001-12-17T09:30:47Z</m:timeStampOrigen>
</m:sendDataExtraccionTraza>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<m:sendDataExtraccionRequest
xmlns:m="http://www.mop.cl/controlextraccion/xsd/datosExtraccion/SendDataExtraccionRequest">
<m:dataExtraccionSubterranea>
<m:fechaMedicion>17-08-1967</m:fechaMedicion>
<m:horaMedicion>00:01:01</m:horaMedicion>
<m:totalizador>1234567891</m:totalizador>
<m:caudal>12345678.12</m:caudal>
<m:nivelFreaticoDelPozo >12345678.12</m:nivelFreaticoDelPozo >
</m:dataExtraccionSubterranea>
</m:sendDataExtraccionRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Tried making it like this:
$client = new SoapClient($url, array('trace' => 1));
$ns = 'http://www.mop.cl/controlextraccion/xsd/datosExtraccion/SendDataExtraccionRequest';
$headerbody = array('codigoDeLaObra' => POZO_PRUEBA,
'timeStampOrigen' => '2020-01-01T09:30:47Z');
$header = new SOAPHeader($ns, 'sendDataExtraccionTraza', $headerbody);
$client->__setSoapHeaders($header);
$args = array( "fechaMedicion" => "10-10-2020",
"horaMedicion" => "10:01:01",
"totalizador" => "1234567891",
"caudal" => "12345678.12",
"nivelFreaticoDelPozo" => "12345678.12"
);
$res = $client->__soapCall('sendDataExtraccionOp', [
new SoapParam("10-10-2020", "fechaMedicion"),
new SoapParam("10:01:01", "horaMedicion"),
new SoapParam("1234567891", "totalizador"),
new SoapParam("12345678.12", "caudal"),
new SoapParam("1", "nivelFreaticoDelPozo")]);
I am getting an error as result, so i've managed to htmlentities the $client->__getLastRequest() and seeing this outcome
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.mop.cl/controlextraccion/xsd/datosExtraccion/SendDataExtraccionRequest">
<SOAP-ENV:Header>
<ns1:sendDataExtraccionTraza>
<ns1:codigoDeLaObra>OB-0202-251</ns1:codigoDeLaObra>
<ns1:timeStampOrigen>2020-01-01T09:30:47Z</ns1:timeStampOrigen>
</ns1:sendDataExtraccionTraza>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:sendDataExtraccionRequest/>
<horaMedicion>10:01:01</horaMedicion>
<totalizador>1234567891</totalizador>
<caudal>12345678.12</caudal>
<nivelFreaticoDelPozo>1</nivelFreaticoDelPozo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I am missing a <m:dataExtraccionSubterranea> parent inside the senddataExtraccionRequest, and i can't figure how to create it.
Any hint?

Related

How to remove the namespace of SOAPHeaderElement using PHP

So basically I want the SOAP header to be like this:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://xfire.super8.com/CrsService" xmlns:ns2="http://www.gzjhotel.com:9999/iPegasus/services/CrsService?wsdl">
<SOAP-ENV:Header>
<AuthenticationToken>
<Username>xxx</Username>
<Password>xxx</Password>
</AuthenticationToken>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:getHotelList>
<ns1:city>GZ</ns1:city>
</ns1:getHotelList>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But instead, now I have this:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://xfire.super8.com/CrsService" xmlns:ns2="http://www.gzjhotel.com:9999/iPegasus/services/CrsService?wsdl">
<SOAP-ENV:Header>
<ns2:AuthenticationToken SOAP-ENV:actor="http://schemas.xmlsoap.org/soap/actor/next">
<Username>xxx</Username>
<Password>xxx</Password>
</ns2:AuthenticationToken>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:getHotelList>
<ns1:city>GZ</ns1:city>
</ns1:getHotelList>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And this is the php code that I'm currently using:
$headerParam = [
'Username' => $username,
'Password' => $password,
];
$client = new \SoapClient($requestUrl, ['trace' => true]);
$authvalues = new \SoapVar($headerParam, SOAP_ENC_OBJECT);
$header = new \SoapHeader($requestUrl, 'AuthenticationToken', $authvalues,
false, SOAP_ACTOR_NEXT);
$client->__setSoapHeaders($header);
$r = $client->getHotelList(['city' => 'GZ']);
Where have I gone wrong? I seems unable to construct the header properly.
I have solved it, by using XSD_ANYXML
$headerParam = "<AuthenticationToken><Username>$username</Username><Password>$password</Password></AuthenticationToken>";
$client = new \SoapClient($requestUrl, ['trace' => true]);
$authvalues = new \SoapVar($headerParam, XSD_ANYXML);
$header = new \SoapHeader($requestUrl, 'AuthenticationToken', $authvalues, false, SOAP_ACTOR_NEXT);
$client->__setSoapHeaders($header);
Thanks to :enter link description here

SOAP with literal xml in php

How do i create this specific xml output by SOAP using SoapHeader and __setSoapHeaders? I can't do the same XML like i want. i don't want this ns1 and ns2 in envelope tag, and in header i need this Action SOAP-ENV:mustUnderstand="1" ...
This is my code:
try{
$client = new SoapClient('https://thesite.com.br/wcf/SvcContratos.svc?wsdl', array('trace' => 1,'use' => SOAP_LITERAL));
$usuario='user_1';
$senha='1234';
$tipo='1';
$header = new SoapHeader("http://schemas.microsoft.com/ws/2005/05/addressing/none","Action", "http://tempuri.org/ISvcContratos/GerarToken");
$client->__setSoapHeaders($header);
$params = new SoapVar("<objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><d4p1:DsSenha>".$senha."</d4p1:DsSenha><d4p1:DsUsuario>".$usuario."</d4p1:DsUsuario><d4p1:IdTipoConsulta>".$tipo."</d4p1:IdTipoConsulta></objLogin>", XSD_ANYXML);
$data = $client->GerarToken($params);
echo $client->__getLastRequest();
}catch(SoapFault $fault){
echo $client->__getLastRequest()."<br>".$fault->getMessage();
}
With this php code i had this wrong XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://schemas.microsoft.com/ws/2005/05/addressing/none">
<SOAP-ENV:Header>
<ns2:Action>http://tempuri.org/ISvcContratos/GerarToken</ns2:Action>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<GerarToken>
<objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<d4p1:DsSenha>1234</d4p1:DsSenha>
<d4p1:DsUsuario>user_1</d4p1:DsUsuario>
<d4p1:IdTipoConsulta>1</d4p1:IdTipoConsulta>
</objLogin>
</GerarToken>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I need to send this XML by soap:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ISvcContratos/GerarToken</Action>
</s:Header>
<s:Body>
<GerarToken xmlns="http://tempuri.org/">
<objLogin xmlns:d4p1="http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:DsSenha>1234</d4p1:DsSenha>
<d4p1:DsUsuario>USER_1</d4p1:DsUsuario>
<d4p1:IdTipoConsulta>1</d4p1:IdTipoConsulta>
</objLogin>
</GerarToken>
</s:Body>
</s:Envelope>
well, finally I got an affirmative answer from the server, which opens the doors for me now to try to consume the wsdl follows below the code that I used to solve the problem:
try{
$client = new SoapClient('https://thesite.com.br/wcf/SvcContratos.svc?wsdl', array('trace' => 1,'use' => SOAP_LITERAL, 'style' => SOAP_DOCUMENT,));
$usuario='user_1';
$senha='1234';
$params = new SoapVar("<GerarToken xmlns='http://tempuri.org/'><objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><d4p1:DsUsuario>".$usuario."</d4p1:DsUsuario><d4p1:DsSenha>".$senha."</d4p1:DsSenha><d4p1:IdTipoConsulta>Data</d4p1:IdTipoConsulta></objLogin></GerarToken>", XSD_ANYXML);
$data = $client->GerarToken($params);
$xml = json_decode(json_encode($data),true);
print_r($xml);
echo $client->__getLastRequest();
}catch(SoapFault $fault){
echo $client->__getLastRequest()."<br>".$fault->getMessage();
}

PHP SOAP:How to get param inside xml header and body tag

In PHP I need to create a soap-xml request like
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header xmlns:NS1="urn:UCoSoapDispatcherBase" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<NS1:TAuthenticationHeader xsi:type="NS1:TAuthenticationHeader">
<UserName xsi:type="xsd:string">user</UserName>
<Password xsi:type="xsd:string">pass</Password>
</NS1:TAuthenticationHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<NS2:ExecuteRequest xmlns:NS2="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap">
<ARequest xsi:type="xsd:string">
<?xml version="1.0" encoding="windows-1252"?> <EoCustomLinkRequestDateTime Type="TEoCustomLinkRequestDateTime"></EoCustomLinkRequestDateTime>
</ARequest>
</NS2:ExecuteRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But the problem is to get param in the header (like xmlns:NS1...) and body (like xmlns:NS2...) tags
With my script I get NS1 and NS2 swapped in the first tag after header and body. results in <NS1:TAuthenticationHeader> .... and . </NS2:ExecuteRequest>
My php script:
<?php
$wsdl_url = 'http://xxx.xxx.xxx.xxx:yyyy/wsdl/ICustomLinkSoap';
$location = 'http://xxx.xxx.xxx.xxx:yyyy/soap/ICustomLinkSoap';
$action = 'ExecuteRequest';
$version = SOAP_1_1;
$one_way = 0;
$soapClient = new SoapClient($wsdl_url, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => true,
'encoding' => 'ISO-8859-1',
'exceptions' => true,
));
$auth = (object)array(
'UserName'=>'xxxx',
'Password'=>'yyyy'
);
$header = new SoapHeader($wsdl_url,'TAuthenticationHeader',$auth,false);
$soapClient->__setSoapHeaders($header);
$request1 = '
<ARequest>
<?xml version="1.0" encoding="windows-1252"?>
<EoCustomLinkRequestDateTime Type="TEoCustomLinkRequestDateTime" xsi:noNamespaceSchemaLocation="GdxEoStructures.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</EoCustomLinkRequestDateTime></ARequest>
';
$xmlVar = new SoapVar($request1, XSD_ANYXML);
$result = $soapClient->__SoapCall(
'ExecuteRequest',
array($xmlVar)
);
// show result in xml-file
$f = fopen("./soap-request2.xml", "w");
xx = serialize($soapClient->__getLastRequest());
fwrite($f, $xx);
?>
Result:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap"
xmlns:ns2="http://xxx.xxx.xxx.xxx:yyy/wsdl/ICustomLinkSoap"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<ns2:TAuthenticationHeader>
<UserName>xxxx</UserName>
<Password>yyyy</Password>
</ns2:TAuthenticationHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:ExecuteRequest>
<ARequest>
<?xml version="1.0" encoding="windows-1252"?>
<EoCustomLinkRequestDateTime Type="TEoCustomLinkRequestDateTime" xsi:noNamespaceSchemaLocation="GdxEoStructures.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</EoCustomLinkRequestDateTime>
</ARequest>
</ns1:ExecuteRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Please help to how to do so?
Eric
But the problem is to get param in the header (like xmlns:NS1...) and
body (like xmlns:NS2...) tags With my script I get NS1 and NS2 swapped
in the first tag after header and body. results in
.... and .
It's not a problem, because swapped not only namespaces of elements but also its definitions at the head of SOAP-ENV:Envelope
Your first xml
xmlns:NS1="urn:UCoSoapDispatcherBase"
xmlns:NS2="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap"
Compare with generated by php
xmlns:ns1="urn:UCoSoapDispatcherCustomLink-ICustomLinkSoap"
xmlns:ns2="http://xxx.xxx.xxx.xxx:yyy/wsdl/ICustomLinkSoap"

php SoapClient - make a call with multiple root tags in request

Here's the xml structure that I want to achieve:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:method>
<auth>
...
</auth>
<data>
...
</data>
</ns1:method>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</XML>
My code:
<?php
$client = new SoapClient(null, array('location' => "http://www.test.com/soap.php",
'uri' => "http://tempuri.org/",
'trace' => true,'exceptions' => true,'cache_wsdl' => WSDL_CACHE_NONE));
$request = new stdClass();
$request->auth = new stdClass();
$request->data = new stdClass();
$client->__soapCall("method", array($request));
Please note: SoapClient doesn't allow me to insert anything other than array in second param to the __soapCall method.
So here's what I'm getting:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:method>
<param0 xsi:type="SOAP-ENC:Struct">
<auth xsi:type="SOAP-ENC:Struct"/>
<data xsi:type="SOAP-ENC:Struct"/>
</param0>
</ns1:method>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can see, php added a root tag for my request - param0. Is there a way I can escape this? I need to have 2 "root" elements in that request parameter. How can I achieve that?
did you try this way : $client->__soapCall("method", $request);?
Thanks all for answering. I solved the issue with the help of SoapParam:
$client->__soapCall("method", array(
new SoapParam($request->auth, "auth"),
new SoapParam($request->data, "data"),
));

soap:Envelope SOAP-ENV:Envelope PHP

I'm trying to login to an API using built-in soap functions of PHP. I got a result like this.
[LoginResult]=> false,
[ErrorMsg] => Login failed with the reason : The security object is invalid
This is what required by the API provider.
<?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>
<Login xmlns="http://tempuri.org/Example/Service1">
<objSecurity>
<WebProviderLoginId>test</WebProviderLoginId>
<WebProviderPassword>test</WebProviderPassword>
<IsAgent>false</IsAgent>
</objSecurity>
<OutPut />
<ErrorMsg />
</Login>
</soap:Body>
</soap:Envelope>
&, here is what I was able to produce using functions.
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/Example/Service1">
<SOAP-ENV:Body>
<ns1:Login>
<objSecurity>
<WebProviderLoginId>test</WebProviderLoginId>
<WebProviderPassword>test</WebProviderPassword>
<IsAgent>false</IsAgent>
</objSecurity>
<OutPut/>
<ErrorMsg/>
</ns1:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Here is the code I used to send the request.
<?php
class objSecurity {
function objSecurity($s, $i, $f) {
$this->WebProviderLoginId = $s;
$this->WebProviderPassword = $i;
$this->IsAgent = $f;
}
}
class nextObject {
function nextObject($objSecurity) {
$this->objSecurity=$pobjSecurity;
$this->OutPut=NULL;
$this->ErrorMsg=NULL;
}
}
$url = 'http://example.com/sampleapi/test.asmx?WSDL';
$client = new SoapClient($url, array("soap_version" => SOAP_1_1,"trace" => 1));
$struct = new objSecurity('test', 'test', false);
$data = new nextObject($struct);
$soapstruct2 = new SoapVar($data, SOAP_ENC_OBJECT);
print_r(
$client->__soapCall(
"Login",
array(new SoapParam($soapstruct2, "inputStruct"))
)
);
echo $client->__getLastRequest();
?>
These are the differences I found.
In my request xmlns:xsi is missing.
Requirement starts with <soap:Envelope, But my request starts with <SOAP-ENV:Envelope.
There is an extra xmlns:ns1 in my request.
& The function name tag starts with ns1:.
Please help me to make my request into the required format.
I don't know much about the SOAP and I'm using PHP version 5.3.13 with CakePHP 2.3.0. Sorry, for my bad English.
Here is the solution. :)
<?php
$url = 'http://example.com/sampleapi/test.asmx?WSDL';
$client = new SoapClient($url, array("soap_version" => SOAP_1_1,"trace" => 1));
$user_param = array (
'WebProviderLoginId' => "test",
'WebProviderPassword' => "test",
'IsAgent' => false
);
$service_param = array (
'objSecurity' => $user_param,
"OutPut" => NULL,
"ErrorMsg" => NULL
);
print_r(
$client->__soapCall(
"Login",
array($service_param)
)
);
echo $client->__getLastRequest();
?>
& the request was:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/Example/Service1">
<SOAP-ENV:Body>
<ns1:Login>
<ns1:objSecurity>
<ns1:WebProviderLoginId>test</ns1:WebProviderLoginId>
<ns1:WebProviderPassword>test</ns1:WebProviderPassword>
<ns1:IsAgent>false</ns1:IsAgent>
</ns1:objSecurity>
</ns1:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Thanks to this link.
PHP SOAP Request not right

Categories