PHP Secured Soap connection failure - php

For a customer we're making a (secured) Soap connection with a Dutch asbestos governmental API in PHP (Laravel in our case) but we are struggling with setting it up.
We've received a guide on how to create the required SSL certificate files, mailed them to the company that provides the API, tested the .wsdl file with SSL(with the .pfx file) in SoapUI and everything works perfectly.
But when we use it in PHP's SoapClient, an Exception is thrown mentioning:
"Could not connect to host"
Here's our code:
$wsdl = resource_path("wsdl/testing/portal_1_1.wsdl");
/**
* #see https://www.php.net/manual/en/soapclient.construct.php
*/
$options = [
'location' => 'https://acceptatie.tws-esb.rws.nl/lavs-koppelvlak-bacc',
'local_cert' => resource_path("certs" . DIRECTORY_SEPARATOR . "vdm-lavs.pem"),
'passphrase' => 'Pr0v!de',
'cache_wsdl' => "WSDL_CACHE_NONE",
'trace' => true,
'exceptions' => true,
'soap_version' => SOAP_1_1,
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
])
];
$soapClient = new \SoapClient($wsdl, $options);
$result = $soapClient->__soapCall('statusOpvraging', []);
What I've checked so far is :
Adding allow_self_signed to the ssl configuration
Since the SoapUI uses the .pfx certificate and the local_cert in SoapClient in PHP does not work with .pfx but .pem, we tried both
We tried it in Postman as a regular HTTP call and it works fine
We've tried in an old PHP 5.6 and Laravel 5.3.0 and a new fresh PHP 8.0 with Laravel 8.68.1 configuration, both result in the same error.
Does anybody have any suggestion?

Related

How can I make SoapClient work on a server using nginx

I'm getting an error when using SoapClient() request. No matter which param I use, the issue remains. The server uses Nginx.
The SOAP error: http://lecturaparatodos.cl/soap.php
I check if SOAP enabled, and it's: http://lecturaparatodos.cl/soap-check.php
I chekck OpenSSL, SimpleXMLand all I think I need and it's ok: http://lecturaparatodos.cl/info.php
Here's the sample code in soap.php:
echo time();
$client = new \SoapClient("https://demo.l1nda.nl/api/webservice/?wsdl",
[
"trace" => 1,
"exceptions" => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'soap_version' => SOAP_1_2,
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
);
var_dump($client->__getFunctions());
SOAP error: http://lecturaparatodos.cl/soap.php
SOAP Check: http://lecturaparatodos.cl/soap-check.php
PHP info: http://lecturaparatodos.cl/info.php
I try everything I found on Google, here and other forums. All required modules are installed, updated and nothing seems to work. If I move to another server, works perfectly. I need to keep the website on the current server that's why I ask for advice.
Thanks!

php SoapClient fails to connect, but command line curl call works

I have a LAMP server (#1) that is communicating via soap with another server (#2) via WSDL. If I issue a curl call on the command line of server 1 to the URL of server 2, it works fine and get the appropriate WSDL response, but a php soapclient to the same URL is getting a "failed to load external entity" error. This was working before when we had a self signed certificate on server 2, but quit working about the same time we upgraded to a CA certificate.
Funny thing is this server is load balanced with another server at a different location (different OS, but same php code/database) and the second server isn't having any issues at all.
Here is the code I am using for the soap client:
function getSoapClient(){
ini_set("soap.wsdl_cache_enabled", 0);
// standard soap client for application service
$post_url = lum_getString("[CAMPAIGN_POST_URL]").
"?enterprise=".lum_getString("[CAMPAIGN_ENTERPRISE]").
"&company=".lum_getString("[CAMPAIGN_COMPANY]");
$options = array(
'trace' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => 1,
'verifypeer' => false,
'verifyhost' => false,
'allow_self_signed' => true,
'login' => lum_getString("[CAMPAIGN_POST_ID]"),
'password' => lum_getString("[CAMPAIGN_POST_LC]"),
);
$context = stream_context_create(
array(
'user_agent' => 'PHPSoapClient',
'ssl' => array(
'verify_peer' => false,
'allow_self_signed' => true,
),
'https' => array(
'curl_verify_ssl_peer' => false,
'curl_verify_ssl_host' => false,
)
)
);
$options['stream_context'] = $context;
$client = new SoapClient($post_url."&wsdl",$options);
return $client;
}
The curl and soapclient are using the same ports so it shouldn't be a firewall issue.
Any help in identifying the issue or helping me figure what is wrong is greatly appreciated.
Turns out it appears to be a firewall issue. I opened up all access from server 1 to server 2 and things started working. Not too sure what the issue was. I reduced the options to just the login and password, it's still working. Firewalls are often the answer.
Any ideas regarding the firewall, the correct ports, and why it wasn't working is much appreciated.
Why was cURL working and SOAP not?

SoapClient "Could not connect to host" exception with one WSDL API

I have a problem with one WSDL connection with API, after moving to a new OVH VPS machine, so it's maybe some kind of strange misconfiguration.
Other WSDL that I use with SoapClient, work fine, without problems. I'm able to use file_get_contents on the address, but when I use SoapClient I gives a exception "Could not connect to host", when trying to use a procedure from that API.
Any ideas? I've tried some stream_context with some SSL options. What's the funniest, on the other OVH VPS is working fine.
System is Debian 8 with PHP 5.6.19 onboard.
Addresss of the WSDL is here: https://api-test.paczkawruchu.pl/WebServicePwR/WebServicePwRTest.asmx?WSDL
After consultation with WSDL provider, and checking logs on both sides, we found the anwser. It looks like PHP 5.6 have some problems, and you have to change parameters to SOAP 1.2. This resolved finally the problem. Resolution could be found here, in first comment:
SOAP PHP fault parsing WSDL: failed to load external entity?
// 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) );
$oSoapClient = new SoapClient ( $url . "?WSDL", $params );

Disable certificate verification in PHP SoapClient

Summary:
Is there a way to force the built in SoapClient-class in PHP to connect over HTTPS to a server with an invalid certificate?
Why would I want to do that?
I have deployed a new application on a server that has no DNS entry or certificate yet. I want to try connecting to it with a SoapClient before setting up the DNS entry and fixing the certificate, and the most reasonable way to do this seems to be to just make the client ignore the certificate during testing.
Don't I realise that this is a huge security risk?
This is only for testing. When the service goes into production, there will be a valid certificate in place, and the client will be forced to validate it.
SoapClient takes a stream context in its parameters, which you can create yourself. That way you can control almost every aspect of the transport layer:
$context = stream_context_create([
'ssl' => [
// set some SSL/TLS specific options
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
$client = new SoapClient(null, [
'location' => 'https://...',
'uri' => '...',
'stream_context' => $context
]);
Documentation:
stream_context_create() Docs
HTTP context options Docs
SSL context options Docs
The accepted answer works but only in the non-WSDL mode. If you try to use this in the WSDL mode (i. e. you pass a WSDL file url as the first argument) you will face the fact that the stream context is ignored when downloading WSDL files. So if the WSDL file is also located on a server with broken certificate, it will fail, most likely throwing the message failed to load external entity. See more here and here.
As suggested, the simplest way around is to download the WSDL file manually and pass the local copy to the SoapClient. You can download it for example with file_get_contents using the very same stream context from the accepted answer.
Note that you will also have to do this when creating a SoapServer.
The correct list for PHP 5.6.8 is
'ssl' => array('verify_peer_name'=>false, 'allow_self_signed' => true),
"verify_peer"=>false,
"verify_peer_name"=>false,
This is working on php 5.6.x;
$arrContextOptions=stream_context_create(array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
)));
$this->client = new \SoapClient("https://tests.com?WSDL",
array(
//"soap_version" => SOAP_1_2,
"trace" => 1, // enable trace to view what is happening
"exceptions" => 0, // disable exceptions
"cache_wsdl" => 0, // disable any caching on the wsdl, encase you alter the wsdl
"stream_context" => $arrContextOptions
)
);
or if you want you can add to cyrpto method
$arrContextOptions=stream_context_create(array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
));

How to get SSL connection with PHP SOAP extension?

When trying to connect to an SSL endpoint with PHP SOAP extension on Windows I am getting the SoapFault "Could not connect to host". Client certificate is not a requirement of the service.
$sslOptions = array(
'ssl' => array(
'cafile' => "c:/inetpub/wwwroot/eServices/Server_DSA_Public_Certificate.pem",
'allow_self_signed' => true,
'verify_peer' => false,
),
);
$sslContext = stream_context_create($sslOptions);
$clientArguments = array(
'stream_context' => $sslContext,
'trace' => true,
'exceptions' => true,
'encoding' => 'UTF-8',
'soap_version' => SOAP_1_1,
);
$oClient = new WSSoapClient("c:/inetpub/wwwroot/eServices/svc.wsdl", $clientArguments);
// WSSoapClient extends SoapClient to add a WS-Security UsernameToken header
$oClient->__setUsernameToken("myusername", "mypassword");
return $oClient->__soapCall($operation, $request);
I converted to .pem format a self-signed server certificate (.cer) that I was given and am passing that in as 'cacert'.
Am able to access the service perfectly using soapUI with the same wsdl.
Question: Do I need to put the server certificate into some trust store? I did already use Internet Explorer to add it into 'Trusted Root Certification Authorities'.
Question: By passing in 'cacert', is it enough for PHP to know it can trust the server?
Any help or suggestions would be much appreciated.
When I ran the code under Apache (using XAMPP) it worked first time. My problem must have been somewhere in the configuration of IIS or PHP.
Setting cacert was superfluous.

Categories