Sending and requesting SOAP - php

I'm trying to use an example that I found on the internet to send and receive from xml to a SOAP url, but I could not understand very well how SOAP works.
Can anyone help me? Here is the code that I'm passing:
$xml = <<<XML
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:cad="http://servicos.saude.gov.br/cadsus/v5r0/cadsusservice" xmlns:cnes="http://servicos.saude.gov.br/wsdl/mensageria/v5r0/cnesusuario" xmlns:fil="http://servicos.saude.gov.br/wsdl/mensageria/v5r0/filtropesquisa" xmlns:nom="http://servicos.saude.gov.br/schema/corporativo/pessoafisica/v1r2/nomecompleto" xmlns:nom1="http://servicos.saude.gov.br/schema/corporativo/pessoafisica/v1r0/nomefamilia" xmlns:cpf="http://servicos.saude.gov.br/schema/corporativo/documento/v1r2/cpf" xmlns:mun="http://servicos.saude.gov.br/schema/corporativo/v1r2/municipio" xmlns:uf="http://servicos.saude.gov.br/schema/corporativo/v1r1/uf" xmlns:tip="http://servicos.saude.gov.br/schema/corporativo/documento/v5r0/tipodocumento"><soap:Header><wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:UsernameToken wsu:Id="UsernameToken-F6C95C679D248B6E3F143032021465917"><wsse:Username>CADSUS.CNS.PDQ.PUBLICO</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">kUXNmiiii#RDdlOELdoe00966</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">KkB/ki6qUjcZpGNqL4monw==</wsse:Nonce><wsu:Created>2015-04-29T15:10:14.659Z</wsu:Created></wsse:UsernameToken></wsse:Security></soap:Header><soap:Body><cad:requestPesquisar><cnes:CNESUsuario><cnes:CNES>6963447</cnes:CNES><cnes:Usuario>LEONARDO</cnes:Usuario><!--Optional:--><cnes:Senha>?</cnes:Senha></cnes:CNESUsuario><fil:FiltroPesquisa><fil:CPF><cpf:numeroCPF>66105234368</cpf:numeroCPF></fil:CPF><fil:tipoPesquisa>IDENTICA</fil:tipoPesquisa></fil:FiltroPesquisa><cad:higienizar>0</cad:higienizar></cad:requestPesquisar></soap:Body></soap:Envelope>
XML;
$wsdl = 'https://servicoshm.saude.gov.br/cadsus/CadsusService/v5r0?wsdl';
$client = new SoapClient($wsdl, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'cache_ttl' => 86400,
'login'=> "CADSUS.CNS.PDQ.PUBLICO",
'password'=> "kUXNmiiii#RDdlOELdoe00966",
'trace' => true,
'exceptions' => true,
));
$xmlVar = new SoapVar($xml, XSD_ANYXML);
$client->getCustomerInfo($xml);
Error:
Fatal error: Uncaught SoapFault exception: [Client] Function ("getCustomerInfo") is not a valid method for this service in /home/itconect/www/sisam/testeJ.php:18 Stack trace: #0 /home/itconect/www/sisam/testeJ.php(18): SoapClient->__call('getCustomerInfo', Array) #1 /home/itconect/www/sisam/testeJ.php(18): SoapClient->getCustomerInfo('
I have another doubt in this code. Would I receive the result or do I have to supplement it with something yet?

If you are using the WSDL of your code, than this SOAP service does not have a method called "getCustomerInfo". The methods according to the WSDL are pesquisar, consultar, incluir, atualizar, alterarSituacao and calcularGrauDeQualidade.
I'd also recommend using the php helpers instead of writing xml yourself (Examples: How to make a PHP SOAP call using the SoapClient class).
Edit: A very basic example
<?php
$wsdl = 'http://www.webservicex.net/BibleWebservice.asmx?WSDL';
$client = new SoapClient($wsdl, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => true,
'exceptions' => true,
));
$keyword = new StdClass();
$keyword->BibleWords = "god";
$result = $client->GetBibleWordsbyKeyWord($keyword);
var_dump($result);

Related

PHP SOAP Request, remove ns1: et ns:2 from request

I would like to remove ns:1 and ns:2 from my Soap request.
Actually I got this when it is generated:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="*********" xmlns:ns2="**************"><SOAP-ENV:Header><ns2:shp:sc><user>***</user><password>*******</password><companyName>******</companyName><autoCommit>***</autoCommit></ns2:shp:sc></SOAP-ENV:Header><SOAP-ENV:Body><ns1:p_put_order><put_orderRequest><shop_order>TEST2</shop_order><shop_account>100000</shop_account><order_comment>test</order_comment></put_orderRequest></ns1:p_put_order></SOAP-ENV:Body></SOAP-ENV:Envelope>
Then in my php file, I got :
$options = array("soap_version" => SOAP_1_1,
"encoding" => "UTF-8",
"trace" => true,
"exceptions" => true,
'cache_wsdl' => WSDL_CACHE_NONE,
);
$client = new SoapClient($soapURL, $options);
$header = new SoapHeader($soapURL,'shp:sc',$auth, false);
$headerSoap = $client->__setSoapHeaders($header);
I tried to use, SoapVar(), __doRequest() and none of those works ...
Have you got an idea about that ?
Thank you for your help
$header = new SoapHeader($soapURL,'shp:sc',$auth, false);
Please remove $soapURL from here. First argument is suppose to be namespace. Please see documentation here: http://php.net/manual/en/class.soapheader.php . If this fail please post WSDL file.

SOAP Fault ; unable to handle

I've got error: Unable to handle request without a valid action parameter.
I want to just login by SOAP server.
My wsdl is here: https://demo.krd.pl/Siddin/2.1/Import.asmx?WSDL
and my code is like:
$client = new SoapClient('https://demo.krd.pl/Siddin/2.1/Import.asmx?WSDL', array(
'location' => 'https://demo.krd.pl/Siddin/2.1/Import.asmx?WSDL',
'trace' => 1,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'soap_version' => SOAP_1_2
));
$auth = new stdClass();
$auth->UserName = 'aaa';
$auth->Password = 'bbb';
$auth = new SoapVar($auth, SOAP_ENC_OBJECT, 'complexType', null, 'LoginRequest', 'types');
$result = $client->login($auth);
die(var_dump($result));
As You can see, I want to invoke method 'login' on this SOAP server with parameter LoginRequest - it's described in wsdl file. How do that?
without a valid action parameter.
This means that your php soap client is not able to make up the needed SOAPAction http header. You WSDL defines for login method this:
soapAction="http://Siddin.ServiceContracts/2006/09/Login"
There are a number of reasons possible for it not to add the header, e.g. this one:
'soap_version' => SOAP_1_2,
Try changing it to
'soap_version' => SOAP_1_1,
In case that doesnt resolve it, i recommend you to start general soap debugging by tracing the message being sent (including http headers) and compare it to a working request that you can set up e.g. using SoapUI https://www.soapui.org/

PHP Soap Fault: Could not connect to host

There are many question is similar like this. But I didn't got the solution when tried. Find my codes below:
Soap XML:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pos="http://PostWebService.org/">
<soap:Header>
<pos:Authentication>
<!--Optional:-->
<pos:UserName>xxxxxxx</pos:UserName>
<!--Optional:-->
<pos:Password>yyyyyyyy</pos:Password>
</pos:Authentication>
</soap:Header>
<soap:Body>
.....
</soap:Body>
</soap:Envelope>
PHP Code:
$client = new SoapClient({soapurl},$params);
$auth = new stdClass();
$auth->UserName = 'xxxxxxx';
$auth->Password = 'yyyyyyyy';
$header = new SoapHeader('NAMESPACE','Authentication',$auth,false);
$client->__setSoapHeaders($header);
$result = $client->__soapCall('{soap function}',array()); // when this line executes it throws me the error "Could not connect to the host"
Please note, My Soap URL is in https. I tried lots other solutions from stack overflow but none of them work. I want anyone tell me why i am getting that error and what i did wrong in the request.
Another Code tried but still the same:
$opts = array(
'ssl' => array('ciphers'=>'RC4-SHA', 'verify_peer'=>false, 'verify_peer_name'=>false)
);
// SOAP 1.2 client
$params = array ('encoding' => 'UTF-8', 'verifypeer' => false, 'verifyhost' => false, 'soap_version' => SOAP_1_2, 'keep_alive' => false,'trace' => 1, 'exceptions' => 1, "connection_timeout" => 180, 'stream_context' => stream_context_create($opts) );
$client = new SoapClient({soapurl},$params);
//Remaining as the same above
When I tried with SoapUI it gives me a response.
Finally, I got the solution,
I found the problem. The host I was trying to connect is redirecting to another domain name. For some reason, PHP 5.6 doesn't carry the location automatically. So i defined the same soap url in the location options.
Eg:
$params = array('location' => {soapurl});
$client = new SoapClient({soapurl},$params);
Thanks for your time. :)
This solution worked for me.
E.g.
$client = new SoapClient('https://example.com/webservice.php?wsdl');
$client->__setLocation('https://www.somethirdparty.com');

Anything wrong with this soapclient request?

Can anyone tell me a reason the following code shouldn't work, assuming it points to the right external files and calls the correct functions?
<?php
$xmlstr = simplexml_load_file("file.xml");
$wsdl = 'file.wsdl';
$client = new SoapClient($wsdl, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'cache_ttl' => 86400,
'trace' => true,
'exceptions' => true,
));
$xmlVar = new SoapVar($xmlstr, XSD_ANYXML);
$client->DoFunction($xmlstr);
The xml file it points to is an exact output of what i should send the server to get a response but all I get is an error 'Fatal error: Uncaught SoapFault exception' talking about the header can't be null. Which sounds like it isn't getting the authentication details but they are all correct. Is there anything I'm obviously doing wrong here?

Use Cookies in PHP soap calls

I need to use cookies in my PHP calls, currently i am using the below request:
$publish_Data ='<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<publishData xmlns="http://www.birst.com/">
<token>'.$login_token.'</token>
<spaceID>'.$space_id.'</spaceID>
<date>'.$date.'</date>
</publishData>
</soap:Body>
</soap:Envelope>';
$location_URL = "https://app2104.bws.birst.com/CommandWebService.asmx?wsdl";
$action_URL = "http://www.birst.com/publishData";
$client = new SoapClient(null, array(
'location' => $location_URL,
'uri' => "",
'trace' => 1,
));
try{
$publish_data_response = $client->__doRequest($publish_Data,$location_URL,$action_URL,1);
//Get response from here
//print_r($publish_data_response);
}catch (SoapFault $exception){
var_dump(get_class($exception));
var_dump($exception);
}
I need somehow to enable and use cookies in order in next calls that will follow.
i tried using
sessions_start()
but it didn't help - In Java, something called session_maintain_property is used which allows for the program to use cookies, how do i do the same in PHP?
Problem was solved.
the issue was with the SoapClient, when i was sending a request i created a new SoapClient for every call and that gave me a different cookies per each call - kind of a different session.
basically you need to define the soap call only once:
$client = new SoapClient(null, array(
'location' => $location_URL,
'uri' => "",
'trace' => 1,
));
and use the same client for the rest of the calls.

Categories