How to create a SOAP request using PHP with a XML example? - php

I have the XML below:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:proc="http://servicos.saude.gov.br/sigtap/v1/procedimentoservice" xmlns:grup="http://servicos.saude.gov.br/schema/sigtap/procedimento/nivelagregacao/v1/grupo" xmlns:sub="http://servicos.saude.gov.br/schema/sigtap/procedimento/nivelagregacao/v1/subgrupo" xmlns:com="http://servicos.saude.gov.br/schema/corporativo/v1/competencia" xmlns:pag="http://servicos.saude.gov.br/wsdl/mensageria/v1/paginacao">
<soap:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="Id-0001334008436683-000000002c4a1908-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>SIGTAP.PUBLICO</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">sigtap#2015public</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body>
<proc:requestPesquisarProcedimentos>
<grup:codigoGrupo>05</grup:codigoGrupo>
<sub:codigoSubgrupo>04</sub:codigoSubgrupo>
<com:competencia>201501</com:competencia>
<pag:Paginacao>
<pag:registroInicial>01</pag:registroInicial>
<pag:quantidadeRegistros>20</pag:quantidadeRegistros>
<pag:totalRegistros>100</pag:totalRegistros>
</pag:Paginacao>
</proc:requestPesquisarProcedimentos>
</soap:Body>
</soap:Envelope>
How to create a PHP request?
I am trying:
$soapClientOptions = array(
'Username' => 'SIGTAP.PUBLICO',
'Password' => 'sigtap#2015public'
);
$client = new SoapClient("https://servicoshm.saude.gov.br/sigtap/ProcedimentoService/v1?wsdl", $soapClientOptions);
$params = array(
'codigoGrupo' => '05',
'competencia' => '201901',
'Paginacao' => array(
'registroInicial' => '01',
'quantidadeRegistros' => '20',
'totalRegistros' => '100'
)
);
$response = $client->__soapCall("pesquisarProcedimentos", array($params));
var_dump($response);
I am noob on SOAP and I have no idea to how create a correct code. The code that I use is not working. I have a Forced circuit exception.
Any help?

If you don’t want to wonder how to construct the PHP request using an OOP approach + the native PHP SoapClient class with autocompletion in PhpStorm or any decent IDE, I strongly advise you to use a WSDL to PHP generator.
Try the https://github.com/WsdlToPhp/PackageGenerator.
For the WsSecurity header, you can also use the https://github.com/WsdlToPhp/WsSecurity.

Related

PHP SOAP client not creating body

After over a half a day of trying and reading tutorials on creating a simple SOAP client, I am no closer to retrieving a request from API I attempting to work with.
WSDL: http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL
I can make the request from SOAP UI with the following simple SOAP request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://publicapi.ekmpowershop.com/">
<soapenv:Header/>
<soapenv:Body>
<pub:GetOrders>
<!--Optional:-->
<pub:GetOrdersRequest>
<!--Optional:-->
<pub:APIKey>myApiKey</pub:APIKey>
</pub:GetOrdersRequest>
</pub:GetOrders>
</soapenv:Body>
</soapenv:Envelope>
This above returns the expected data.
When it comes to translating the request into a PHP I have the following:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array('GetOrdersRequest' => array(
'APIKey' => 'myApiKey'
)
);
$resp = $client->GetOrders();
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
If put the $param into the GetOrders function then, it breaks and nothing happens.
Even if I use an array in the $client->GetOrders(array('someArry' => $param)) then response and request still always looks the same and looks like the body of the SOAP request is never created:
Request:
?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://publicapi.ekmpowershop.com/"><SOAP-ENV:Body><ns1:GetOrders/></SOAP-ENV:Body></SOAP-ENV:Envelope>
Response:
<?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><GetOrdersResponse xmlns="http://publicapi.ekmpowershop.com/"><GetOrdersResult><Status>Failure</Status><Errors><string>Object reference not set to an instance of an object.</string></Errors><Date>2017-04-03T16:00:42.9457446+01:00</Date><TotalOrders>0</TotalOrders><TotalCost xsi:nil="true" /></GetOrdersResult></GetOrdersResponse></soap:Body></soap:Envelope>
If anyone can shed some light on what I am doing wrong here that would be real big help?
P.S My experience of SOAP in PHP is limited as I am used to SOAP in a java env. Thanks
You need to pass the parameters into the $client->GetOrders() call. Also the WSDL defines some required parameters, so a minimal example is something like:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array(
'GetOrdersRequest' => array(
'APIKey' => 'dummy-key',
'CustomerID' => 1,
'ItemsPerPage' => 1,
'PageNumber' => 1,
)
);
$resp = $client->GetOrders($param);
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
This gives the error response:
<Errors><string>Invalid character in a Base-64 string.</string></Errors>
which presumably is because my API key is invalid.

WSDL interpret supossed operation parameters

I'm trying to invoque this operation getCountries but I can't figure out reading the WSDL file wich parameters are needed and in which structured way:
http://webservice.nizacars.es/Rentway_WS/getCountries.asmx?WSDL
I've already tried with:
$this->soap_client->getCountries(
array(
'countriesRequest' => array(
'companyCode' => $this->login,
'allCountries' => true
)
)
)
$this->soap_client->getCountries(
array(
'companyCode' => $this->login,
'allCountries' => true
)
)
$this->soap_client->getCountries(
'companyCode' => $this->login,
'allCountries' => true
)
But it seems I'm not matching the specs, since I'm getting a "[Server was unable to process request. ---> Object reference not set to an instance of an object.]"
The final request with SoapClient::__getLastRequest is:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.jimpisoft.pt/Rentway_Reservations_WS/getCountries">
<SOAP-ENV:Body>
<ns1:getCountries/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Edit, Solution:
$data = array(
'getCountries' => array(
'objRequest' => array(
'companyCode' => $this->login,
'allCountries' => true
)
)
);
$result = #$this->_client->__call('getCountries',$data);
wich parameters are needed and in which structured way:
You can use the soapUI-tool for generating a valid soap-request and response.
I propose to compare your soap-request (by logging it) with the one generated by soapUI:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:get="http://www.jimpisoft.pt/Rentway_Reservations_WS/getCountries">
<soap:Header/>
<soap:Body>
<get:getCountries>
<!--Optional:-->
<get:objRequest>
<!--Optional:-->
<get:companyCode>?</get:companyCode>
<get:allCountries>?</get:allCountries>
</get:objRequest>
</get:getCountries>
</soap:Body>
</soap:Envelope>

How to set SOAP header on PHP to authenticate a web service?

I need to set the SOAP header into this format:
<soapenv:Header>
<wsse:Security 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-45">
<wsse:Username>XXXXX</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXX</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">OxWtCYYj1cX7HiZeMEqorw==</wsse:Nonce>
<wsu:Created>2013-09-18T07:25:50.227Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
I've tried authenticating the webservice using the code below but it didn't work.
$momurl = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/";
$client = new SoapClient(
"$momurl",
array(
'trace' => 1,
'login' => $username,
'password' => $password
)
);
Any help would be appreciated. Thanks in advance.
You need to use SoapClient::__setSoapHeader(). Like this:
$security = array(
'Username'=>'XXXXX',
'Password'=>'XXXXX',
'Nonc'=> 'OxWtCYYj1cX7HiZeMEqorw==',
'Created' => '2013-09-18T07:25:50.227Z',
'UsernameToken' => NULL
);
$header = new SoapHeader('wsse','Security',$security, false);
$client->__setSoapHeaders($header);

How do I create this xml with PHP's SoapClient?

From debugging another application, I found that it sends the following xml to a soap server (code parts in this example are minimized, the xml is about 200 lines long:
<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" xmlns:m0="http://schemas.ccs.nl/datacatalogus/modellen/modelrelatie">
<SOAP-ENV:Header>
<m:header xmlns:m="http://schemas.ccs.nl/soap">
<m:account>account</m:account>
<m:naam>naam_header</m:naam>
<m:wachtwoord>wachtwoord</m:wachtwoord>
<m:bedrijfsnummer>bedrijfsnummer</m:bedrijfsnummer>
<m:tussenpersoonnummer>tussenpersoonnummer</m:tussenpersoonnummer>
</m:header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<m:RelatieMuteren xmlns:m="http://schemas.ccs.nl/services/relatieservice">
<m:relatie pc="W">
<m0:adres>adres</m0:adres>
</m:relatie>
</m:RelatieMuteren>
</SOAP-ENV:Body>
Obviously, the part between <m0:adres></m0:adres> is a lot larger and corresponds to my data that is stored in an array. However, if I try to send the request using __soapCall, PHP builds the following request:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://schemas.ccs.nl/datacatalogus/modellen/modelrelatie"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://schemas.ccs.nl/services/relatieservice" xmlns:ns3="http://schemas.ccs.nl/soap">
<SOAP-ENV:Header>
<ns3:header>
<ns3:account>account</ns3:account>
<ns3:naam>naam</ns3:naam>
<ns3:wachtwoord>wachtwoord</ns3:wachtwoord>
<ns3:bedrijfsnummer>bedrijfsnummer</ns3:bedrijfsnummer>
<ns3:tussenpersoonnummer>tussenpersoonnummer</ns3:tussenpersoonnummer>
</ns3:header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:RelatieMuteren>
<ns2:relatie pc="I">
<ns1:adres>Postbus 53</ns1:adres>
</ns2:relatie>
</ns2:RelatieMuteren>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can see, the second xml is quite different from the first. Can anyone explain me why it is different and how I can create the first type of xml?
I use the following code to do the request:
$client = new SoapClient( "http://www.cdsverzekeringen.nl/ws_prod/services/RelatieService.asmx?WSDL", array( 'trace' => 1 ) );
$aHeader = array(
'account' => "PRIVATE",
'naam' => "PRIVATE",
'wachtwoord' => "PRIVATE",
'bedrijfsnummer' => "PRIVATE",
'tussenpersoonnummer' => "PRIVATE",
);
$client->__setSoapHeaders( new SoapHeader( "http://schemas.ccs.nl/soap", 'header', $aHeader ) );
$vtResult = $client->__soapCall( "RelatieMuteren", array( $aRelatieInfo ) );
The $aRelatieInfo array is formatted like this:
array
'relatie' =>
array
'adres' => string 'Postbus 53' (length=10)
I hope someone can help me out. Thanks in advance!
How about using SoabVar API like this,
$aHeader = '
<m:header xmlns:m="http://schemas.ccs.nl/soap">
<m:account>account</m:account>
<m:naam>naam_header</m:naam>
<m:wachtwoord>wachtwoord</m:wachtwoord>
<m:bedrijfsnummer>bedrijfsnummer</m:bedrijfsnummer>
<m:tussenpersoonnummer>tussenpersoonnummer</m:tussenpersoonnummer>
</m:header>';
$soap_var_header = new SoapVar( $aHeader , XSD_ANYXML, null, null, null);
$client->__setSoapHeaders( new SoapHeader( "http://schemas.ccs.nl/soap", 'm', $soap_var_header ) );

PHP soap server double encoding html strings

I'm having a problem where my php soap webservice is double encoding strings. For example if I try to return the string O'Test, what I get is O&apos;Test. What I would have expected to see is O&apos;Test. It seems like what is happening is the & itself is getting encoded?
I've look through what phpinfo() returns (this is php version 5.1.6), but I don't see anything obvious.
My code to initialize the soap server uses arrays to automate the initialization of my functions and types but this is the gist of what I'm doing:
$server = new soap_server();
$server->configureWSDL('server', 'urn:sg');
$server->wsdl->addComplexType( 'testCall', 'complexType', 'struct', 'all', '', array( 'name' => 'testName', 'type' => 'xsd:string' ) );
$server->register( $fname,
array( 'request' => 'tns:TestCallRequest' ),
array( 'return' => 'tns:TestCallResponse' ),
'urn:sg',
'urn:sg#testCall' );
$server->service($HTTP_RAW_POST_DATA);
Here is the function represented by $fname above:
function testCall() {
$result = array();
$result[testName] = 'O\'Test';
return $result;
}
Here is the soap request and response that I see from a client perspective:
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sg"><soapenv:Header/><soapenv:Body><urn:testCall soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><urn:TestCallRequest></urn:TestCallRequest></urn:testCall></soapenv:Body></soapenv:Envelope>
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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/" xmlns:tns="urn:sg"><SOAP-ENV:Body><ns1:TestCallResponse xmlns:ns1="urn:sg"><return xsi:type="tns:TestCallResponse"><testName xsi:type="xsd:string">O&apos;Test</testName></return></ns1:TestCallResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

Categories