PHP SOAP with HTTPS webservice and .PEM client certificate - php

How to create PHP SOAP with HTTPS webservice and .PEM client certificate ?
When i did that i received the follow errors:
array(2) { [0]=> string(38) "testeResponse teste(teste $parameters)" [1]=> string(38) "testeResponse teste(teste $parameters)" }
Fatal error: Uncaught SoapFault exception: [wsse:InvalidSecurity] SOAP header missing in D:\Inetpub\wwwroot\SOAP\index.php:47 Stack trace: #0 [internal function]: SoapClient->__call('teste', Array) #1 D:\Inetpub\wwwroot\SOAP\index.php(47): SoapClient->teste(Array) #2 {main} thrown in D:\Inetpub\wwwroot\SOAP\index.php on line 47
The code:
$client = new SoapClient($wsdl, array(
'local_cert' => $localCert,
'passphrase' => $passphrase,
'style' => SOAP_DOCUMENT,
'use' => SOAP_LITERAL,
'exceptions' => true,
'trace' => true)
);
$something = $client->teste(array());
echo $something->testeResult;

The warning is not about the PEM certificate or SSL. It is simply:
SOAP header missing
You find SOAPHeader in the PHP manual. There are some usage examples and user contributed notes in the PHP manual and also here on this website, too. For example:
$auth = array(
'UserName' => 'USERNAME',
'Password' => 'PASSWORD',
'SystemId' => array('_'=> 'DATA', 'Param' => 'PARAM'),
);
$header = new SoapHeader('NAMESPACE','Auth',$auth,false);
$client->__setSoapHeaders($header);
Let me know if you run into problems.

Related

"Uncaught SoapFault exception: [WSDL] SOAP-ERROR: WSDL parsing: Could not load" using ZEND SOAP client

I am having trouble accessing a SOAP service.
I'm using the Zend library, and did the following function:
public function boletoApiBB() {
//$token = $this->boletoApiBBOauth();
$client = new ZendSoapClient("https://cobranca.homologa.bb.com.br:7101/Processos/Ws/RegistroCobrancaService.serviceagent?wsdl");
echo " <br> testing <BR>";
echo("<PRE>");
print_r($client->getOptions());
echo " <br> client <BR>";
print_r($client);
echo " <br> functions <BR>";
print_r($client->getFunctions());
echo("</PRE>");
}
The request is returning me the following result
testing
Array
(
[encoding] => UTF-8
[soap_version] => 2
[wsdl] => https://cobranca.homologa.bb.com.br:7101/Processos/Ws/RegistroCobrancaService.serviceagent?wsdl
)
client!
Zend\Soap\Client Object
(
[classmap:protected] =>
[encoding:protected] => UTF-8
[faultExceptions:protected] => Array
(
)
[lastMethod:protected] =>
[permanentSoapInputHeaders:protected] => Array
(
)
[soapClient:protected] =>
[soapInputHeaders:protected] => Array
(
)
[soapOutputHeaders:protected] => Array
(
)
[soapVersion:protected] => 2
[typemap:protected] =>
[wsdl:protected] => https://cobranca.homologa.bb.com.br:7101/Processos/Ws/RegistroCobrancaService.serviceagent?wsdl
[keepAlive:protected] =>
[sslMethod:protected] =>
[connectionTimeout:protected] =>
[localCert:protected] =>
[location:protected] =>
[login:protected] =>
[passphrase:protected] =>
[password:protected] =>
[proxyHost:protected] =>
[proxyLogin:protected] =>
[proxyPassword:protected] =>
[proxyPort:protected] =>
[streamContext:protected] =>
[style:protected] =>
[uri:protected] =>
[use:protected] =>
[userAgent:protected] =>
[cacheWsdl:protected] =>
[compression:protected] =>
[features:protected] =>
)
functions
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://cobranca.homologa.bb.com.br:7101/Processos/Ws/RegistroCobrancaService.serviceagent?wsdl' : failed to load external entity "https://cobranca.homologa.bb.com.br:7101/Processos/Ws/RegistroCobrancaService.serviceagent?wsdl"
in /var/www/html/vendor/zendframework/zend-soap/src/Client/Common.php:33
Stack trace:
#0 /var/www/html/vendor/zendframework/zend-soap/src/Client/Common.php(33): SoapClient->SoapClient('https://cobranc...', Array)
#1 /var/www/html/vendor/zendframework/zend-soap/src/Client.php(1070): Zend\Soap\Client\Common->__construct(Array, 'https://cobranc...', Array)
#2 /var/www/html/vendor/zendframework/zend-soap/src/Client.php(1245): Zend\Soap\Client->initSoapClientObject()
#3 /var/www/html/vendor/zendframework/zend-soap/src/Client.php(1202): Zend\Soap\Client->getSoapClient()
#4 /var/www/html/global/controllers/boletoControllerGlobal.php(1319): Zend\Soap\Client->getFunctions()
#5 /var/www/html/global/System.php(207 in /var/www/html/vendor/zendframework/zend-soap/src/Client/Common.php on line 33
By reading the SOAP service documentation, there is an OAUTH2 server for authentication. I did the authentication and it returns me a token.
The authentication function:
private function boletoApiBBOauth() {
$client_id = "eyJpZCI6Ijg...";
$secret_key = "eyJpZCI6IjBj...";
$authorization = "Basic ".base64_encode($client_id.":".$secret_key);
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://oauth.hm.bb.com.br/oauth/token',
[
'headers' => [
'authorization' => $authorization
],
'form_params' => [
'grant_type' => "client_credentials",
'scope' => "cobranca.registro-boletos",
]
]
);
$body = $response->getBody();
$resposta = json_decode($body, true);
$token = $resposta["access_token"];
return $token;
}
I send the authentication request to the URL
https://oauth.hm.bb.com.br/oauth/token.
With the token, I have to send the necessary information to the url
https://cobranca.homologa.bb.com.br:7101/registrarBoleto
Following this pattern:
Header
SOAPACTION: registrarBoleto
Authorization:Bearer
aO2TXKd5Mhsg4zpAs3H7ze0YEgk5TdAMeGgIazrsq5TI8EDdAiSryGz/fcGfhMeGgINeLotO+ndhgHVYOsJx87
g==.9iFwrWb9cxwz8i5Jj+YgB+asAn7jJMeGgIHEFtWj4ES503/BUR2fRHNKREAUT22ysR9sMyEoC2FGx/s7B+
wG/P+L6rHxQiMiXAVzUcOj2h0P30QRCWhZwRhp5EulgfnE3Sm5JYal7cXAG3zIP6NehU5Cx5baeiGEbyO2yR
gzyeAiHQ81TTUqOrCxCcRi6XbafVQJdKkwwbecwV5Wggg1cNHz7dZgPXLZdUPm5FDWRIX2+PrRVPrDdC2fgf
oZl2MHunUL6lkaQ9Cg+oPA0Nj9XSKhMeGgIVG3bR8TXgQNkzn02Rl2wJMybEYZXe9yeIjt3PtB9wR/eLNlxZW
OYu8bbDOEvXJ2hz218co3q1KXA7uLhazDgvI8HR9eVpfu7jM39mGLmmwLb15YXpeSg5TVnyE6sdlt5iHdQhI
snucU7qs9ZzsTBcnZk/GZoGqcSdyFoFRIts4+XdgrpRfs9rycZxNG2oNKSdTG0ZDfwo6puBqZeLMZ15MHzx/Zb
x4urjJnVbcsbHoi2iEjch/nHHdpRSDLJmnQni3Ya24rt3HFqnTFQY5TsEL95w0jxLImRxHjb/PK1b5o9+ST/Dfj8k
MsWzaVZGbVqNkpJ7TVkGtbwQgf/kVS4OLrFM6XDJZLNViGtTzKYBIyuUoJahwBcncwRXkwngdcu23qSjbLM
AXcQxYMloZ/jgA3jLGU7Yn
Content-Type: text/xml; charset=uft-8
Payload
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sch="http://www.tibco.com/schemas/bws_registro_cbr/Recursos/XSD/Schema.xsd">
<soapenv:Header/>
<soapenv:Body>
<sch:requisicao>
<sch:numeroConvenio>1014051</sch:numeroConvenio>
<sch:numeroCarteira>17</sch:numeroCarteira>
<sch:numeroVariacaoCarteira>19</sch:numeroVariacaoCarteira>
<sch:codigoModalidadeTitulo>1</sch:codigoModalidadeTitulo>
<sch:dataEmissaoTitulo>01.03.2017</sch:dataEmissaoTitulo>
<sch:dataVencimentoTitulo>21.11.2017</sch:dataVencimentoTitulo>
<sch:valorOriginalTitulo>30000</sch:valorOriginalTitulo>
<sch:codigoTipoDesconto>1</sch:codigoTipoDesconto>
<sch:dataDescontoTitulo>21.11.2016</sch:dataDescontoTitulo>
<sch:percentualDescontoTitulo/>
<sch:valorDescontoTitulo>10</sch:valorDescontoTitulo>
<sch:valorAbatimentoTitulo/>
<sch:quantidadeDiaProtesto>0</sch:quantidadeDiaProtesto>
<sch:codigoTipoJuroMora>0</sch:codigoTipoJuroMora>
<sch:percentualJuroMoraTitulo></sch:percentualJuroMoraTitulo>
<sch:valorJuroMoraTitulo></sch:valorJuroMoraTitulo>
<sch:codigoTipoMulta>2</sch:codigoTipoMulta>
<sch:dataMultaTitulo>22.11.2017</sch:dataMultaTitulo>
<sch:percentualMultaTitulo>10</sch:percentualMultaTitulo>
<sch:valorMultaTitulo></sch:valorMultaTitulo>
<sch:codigoAceiteTitulo>N</sch:codigoAceiteTitulo>
<sch:codigoTipoTitulo>2</sch:codigoTipoTitulo>
<sch:textoDescricaoTipoTitulo>DUPLICATA</sch:textoDescricaoTipoTitulo>
<sch:indicadorPermissaoRecebimentoParcial>N</sch:indicadorPermissaoRecebimentoParcial>
<sch:textoNumeroTituloBeneficiario>987654321987654</sch:textoNumeroTituloBeneficiario>
<sch:textoCampoUtilizacaoBeneficiario/>
<sch:codigoTipoContaCaucao>1</sch:codigoTipoContaCaucao>
<sch:textoNumeroTituloCliente>00010140510000000000</sch:textoNumeroTituloCliente>
<sch:textoMensagemBloquetoOcorrencia>Pagamento disponível até a data de vencimento
</sch:textoMensagemBloquetoOcorrencia>
<sch:codigoTipoInscricaoPagador>2</sch:codigoTipoInscricaoPagador>
<sch:numeroInscricaoPagador>73400584000166</sch:numeroInscricaoPagador>
<sch:nomePagador>MERCADO ANDREAZA DE MACEDO</sch:nomePagador>
<sch:textoEnderecoPagador>RUA SEM NOME</sch:textoEnderecoPagador>
<sch:numeroCepPagador>12345678</sch:numeroCepPagador>
<sch:nomeMunicipioPagador>BRASILIA</sch:nomeMunicipioPagador>
<sch:nomeBairroPagador>SIA</sch:nomeBairroPagador>
<sch:siglaUfPagador>DF</sch:siglaUfPagador>
<sch:textoNumeroTelefonePagador>45619988</sch:textoNumeroTelefonePagador>
<sch:codigoTipoInscricaoAvalista/>
<sch:numeroInscricaoAvalista/>
<sch:nomeAvalistaTitulo/>
<sch:codigoChaveUsuario>1</sch:codigoChaveUsuario>
<sch:codigoTipoCanalSolicitacao>5</sch:codigoTipoCanalSolicitacao>
</sch:requisicao>
</soapenv:Body>
</soapenv:Envelope>
I'm lost in how I should do it.
Could anyone work it out?
You are getting error in the first step of the process, that is fetching the WSDL resource. It's hard to debug this issue but most probable causes are:
the WSDL url is actually not reachable
the server uses not trusted HTTPS certificate
Try getting the WSDL with wget or curl on the same server, ex:
curl -vvv "https://cobranca.homologa.bb.com.br:7101/Processos/Ws/RegistroCobrancaService.serviceagent?wsdl"
For me it returns the error:
SSL certificate problem: self signed certificate in certificate chain
You can either add this self signed certificate to trusted certificates store on your system, or disable SSL verification by setting the proper stream context on the SOAP client with setStreamContext method (I don't know what version of Zend Soap client you are using, so maybe there is no such method in your client):
$client->setStreamContext(
stream_context_create(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]
)
);
Of course the solution with disabling SSL verification is not advised on production system. The service you are connecting to should use valid trusted certificate, nowadays it's free and easy to get one for example from Let's Encrypt.

Fatal Error in Awin API - Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from

I have a very serious issue. I am working witht he API - https://ui.awin.com/awin/affiliate/250615?region=gb and after login into that, i found the api documentation and also php api client zip.
I then downloaded this client zip, updated all my api details and when i run - http://202.131.107.107/searchcycle/ps_client.php , I always get error as below -
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://v6.core.com.productserve.com/ProductServeService.wsdl' : failed to load external entity "http://v6.core.com.productserve.com/ProductServeService.wsdl" in /var/www/html/searchcycle/classes/class.php5Client.php:57 Stack trace: #0 /var/www/html/searchcycle/classes/class.php5Client.php(57): SoapClient->SoapClient('http://v6.core....', Array) #1 /var/www/html/searchcycle/classes/class.php5Client.php(95): Php5Client->__construct(Object(stdClass)) #2 /var/www/html/searchcycle/classes/class.ClientFactory.php(60): Php5Client::getInstance(Object(stdClass)) #3 /var/www/html/searchcycle/ps_client.php(29): ClientFactory::getClient() #4 {main} thrown in /var/www/html/searchcycle/classes/class.php5Client.php on line 57
Can someone please help me in this what is missing from my side.
Add optional SSL parameters.
How to solve:
// options for ssl in php 5.6.5
$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,
'trace' => 1,
'exceptions' => 1,
'connection_timeout' => 180,
'stream_context' => stream_context_create($opts) // Magic happens here
);
$wsdlUrl = $url . '?WSDL';
$oSoapClient = new SoapClient($wsdlUrl, $params);
Please check this question, you would find something useful.

Uncaught SoapFault exception: [HTTP] Could not connect to host...[internal function]

I have a PHP Client.
The Server WSDL is https://34.200.105.231/VENTAS_SOAP/VentaService.svc?wsdl
The webservices generated by the session work without any problems
$params = array('soap_version' => SOAP_1_2);
$ws = new SoapClient(
'https://34.200.105.231/VENTAS_SOAP/VentaService.svc?wsdl',
$params
);
$parametros = array( 'sesion' => array(
'IDSesion' => "44444",
'IDCliente' => "rick",
'IDEmpresa' => "rick",
'IDUsuario' => "user",
'Token' => '',
),'codigo' => 'ac',
'descripcion' => '',
'cantidadRegistros' => '1',
'pagina' => '0'
);
print_r(print_r($ws->__soapCall("ConsultaArticulosServicios",$parametros)));
Not work, Response:
Fatal error: Uncaught SoapFault exception:
[HTTP] Could not connect to host in /home/admin/web/xxxxxxxx/public_html/nusoapfontana/metodos/index3.php:41
Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'https://df-0e7d...', 'VentaServicio/V...', 2, 0)
#1 /home/admin/web/xxxxxxxx/public_html/nusoapfontana/metodos/index3.php(41): SoapClient->__soapCall('ConsultaArticul...', Array)
#2 {main} thrown in /home/admin/web/xxxxxxxx/public_html/nusoapfontana/metodos/index3.php on line 41
If I can access the Login function found here:
Http://34.200.105.231/SID/Service.svc?wsdl
Here I generate the data for the session.

Write username to httpheader for web service authetification with php

My code:
$url = "http://x.x.x.x/ws/special/Service?wsdl";
$context = stream_context_create(array('http' => array( 'username' => "jonas" )));
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));
$res = $client->__soapCall("getinfo",array('counter'=>'10'));
Error:-
Fatal error: Uncaught SoapFault exception: [S:Server] missing parameter
'username' in C:\xampp\htdocs\test.php:7 Stack trace: #0
C:\xampp\htdocs\test.php(7): SoapClient->__soapCall('getAsinfo...', Array)
#1 {main} thrown in C:\xampp\htdocs\test.php on line 7
How to write to http header?

How to change the namespace sent with a SOAP request?

Related to this question: How to request an answer from a SOAP XML API?
I have this PHP script sending the request with the required fields:
$client = new SoapClient('https://live.domainbox.net/?WSDL');
$params = array(
'AuthenticationParameters' => array(
'Reseller' => 'resellername',
'Username' => 'myusername',
'Password' => 'mypassword'
),
'CommandParameters' => array(
'DomainName' => 'mydomain.com',
'LaunchPhase' => 'GA'
)
);
$result = $client->CheckDomainAvailability($params);
print_r($result);
I get the following error message:
Fatal error: Uncaught SoapFault exception: [soap:VersionMismatch]
Possible SOAP version mismatch: Envelope namespace
http://schemas.xmlsoap.org/soap/envelope/ was unexpected.
Expecting http://www.w3.org/2003/05/soap-envelope. in
/home/nosa/public_html/dev/check-domain-availability.php:20 Stack
trace: #0
/home/nosa/public_html/dev/check-domain-availability.php(20):
SoapClient->__call('CheckDomainAvai...', Array) #1
/home/nosa/public_html/dev/check-domain-availability.php(20):
SoapClient->CheckDomainAvailability(Array) #2 {main} thrown in
/home/nosa/public_html/dev/check-domain-availability.php on line 20
I removed and recompiled apache to include the latest SOAP version available using WHM/cPanel.
My question is:
How do I send the correct envelope namespace?
When building your soap_client object, you can pass an option's array, by default, the version will be 1.1 as per php documentation.
The error you have seems to point out that the service might be SOAP 1.2 so you need to set it to 1.2...
$client = new SoapClient('https://live.domainbox.net/?WSDL', array('soap_version' => SOAP_1_2));
Try that out!

Categories