SoapFault exception: [HTTP] "Could not connect" - php

I have a php web site that used to work running on Centos 6.2 but I cannot make it work on Centos 7.
I have Apache 2.4.6 with php 5.4.16
The problem I am having is that php SoapClient gives me a Soap Exception "Could not connect" and my Soap Server is on the same server as my http server. I tried with the server ip address and with 127.0.0.1 and it still does not work.
So I made a simple php test file (test.php) to eliminate all the complexity of my web server
What I don't understand is that it only fails if I run the php file from an HTTP request. (http://my-server/test-soap.php)
If I run the php file from the command line, it works. (php test-soap.php)
If I test with Curl is works. If I use Soap-UI from my Mac (remote machine) it works.
Also, if I run tcpdump , I don't even see php trying to connect. (no packet at all)
Any idea ?
This is the test code.
<?php
if (php_sapi_name() == "cli") {
echo "Running from CLI\n";
$nl="\n";
$_SERVER = array (
'SERVER_ADDR' => '127.0.0.1',
'PHP_AUTH_USER' => '',
'PHP_AUTH_PW' => '',
);
} else {
$nl="<br/>\n";
// Not in cli-mode
}
echo "Start".$nl;
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$http_port = 80;
$ems_soap_port = 8003;
$ems_addr = "127.0.0.1";
$soap_target_ems = "https://$ems_addr:$ems_soap_port";
$wsdl_target_ems = "http://$ems_addr:$http_port/wsdl/gccpa.wsdl";
$uri_ems = "urn:gccpa";
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
));
$client = new SoapClient( $wsdl_target_ems, array(
'stream_context' => $context,
'exceptions'=> true,
'location' => $soap_target_ems ,
'uri' => $uri_ems,
'encoding' => 'UTF-8',
'trace' => true ) );
try
{
$getnodesResult = $client->__soapCall( 'gccpa-getnodes',
array( 'parameters' => array('sessionId' => 0, 'sysId' => 1) ) );
echo "Soap Successull".$nl;
}
catch(SoapFault $exception)
{
echo 'gccpa:'.'getnodes exception: '. $exception ;
unset($client);
}
?>

Related

Soap unable to connect to host using .wsdl file php 7.3 Drupal 8

I am trying to get data from two soap apis here is the code for first api that is working fine.
public static function data_list()
{
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 0);
$host = $host = \Drupal::request()->getSchemeAndHttpHost();
$wsdl = $host . "/modules/custom/upm_api_data/connect/empprof.wsdl";
$client = new \SoapClient($wsdl);
$params = array('ALL' => 'ALL');
$response = $client->__soapCall("ZCIG_FACULTY_STAFF_PROFILE", array($params));
return $response;
}
And there is another api that is showing:
SoapFault: Could not connect to host in SoapClient->__doRequest()
error
public static function images_api()
{
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$host = $host = \Drupal::request()->getSchemeAndHttpHost();
$wsdl = $host . "/modules/custom/upm_api_data/connect/ImageRac.WSDL";
$client = new \SoapClient($wsdl);
$parameters = array('P_PERNR' => "00006629");
$response = $client->__soapCall("ZCIG_IMAGE_WS", array($parameters));
return $response;
}
Second api is working fine in Drupal 7 Php 5.6 but similer code is not working in php 7.3 Drupal 8
I have tried almost every thing related to this issue on internet
like adding this in options
array(
'stream_context'=> stream_context_create(
array(
'ssl'=> array(
'verify_peer'=>false,'verify_peer_name'=>false
)
)
)
)
or
array(
'trace' => 1,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'stream_context' => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
)
)
);
But not luck I have spent many days on it and I am new to Soap. Can you guys please check what I am doing wrong.
Thank you for everyone in advance :)

SoapClient call works on PHP 5.6 but not on PHP 7

I have a soapClient call that works fine with PHP 5.6 (RH6). We are upgrading the system to PHP 7 (RH7 with the same configuration as the previous one) but the same call does not work.
This is my code
$wsdlUrl = "https://THE_URL_I_AM_CALLING/repository/soap/2.1?wsdl";
$sslClientCert = "../../app/config/ssl/ssl_cert.crt";
$sslClientKey = "../../app/config/ssl/ssl_cert.key";
$proxy = 'proxy_http';
$port = 8080;
$contextOptions = [
'ssl' => [
'local_cert' => $sslClientCert,
'local_pk' => $sslClientKey,
'SNI_enabled' => true,
'SNI_server_name' => 'THE_URL_I_AM_CALLING'
]
];
$options= [
"soap_version" => SOAP_1_2,
"features" => SOAP_SINGLE_ELEMENT_ARRAYS,
"stream_context" => stream_context_create($contextOptions),
'proxy_host' => $proxy,
'proxy_port' => $port
];
$client = new SoapClient($wsdlUrl, $options);
try {
// execute the search
$searchResults = $client->searchDocuments([
"text" => "myText",
"hint" => "document"
]);
}
catch (Exception $e) {
echo $e->getMessage();
}
the error I get under PHP 7 is
PHP Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host
if I make the call using CURL it works.
I would rather use clientSoap to make my life easier.
I found the solution. Posting it here so no other soul on earth needs to go through this ordeal.
According to the documentation
http://php.net/manual/en/context.ssl.php#context.ssl.sni-server-name
SNI_server_name (string):
If set, then this value will be used as server name for server name indication. If this value is not set, then the server name is guessed based on the hostname used when opening the stream.
Note: This option is deprecated, in favour of peer_name, as of PHP
5.6.0.
After changing:
'SNI_server_name' => 'THE_URL_I_AM_CALLING'
with
'peer_name' => 'THE_URL_I_AM_CALLING'
It works.

Running PHP SoapServer behind a proxy

I'm attempting to run both a PHP SoapClient and a SoapServer (for Magento) behind a proxy, where the only network traffic allowed out is via the proxy server.
I have got this working with the client as so:
$client = new SoapClient('https://www.domain.co.uk/api/v2_soap/?wsdl=1', [
'soap_version' => SOAP_1_1,
'connection_timeout' => 15000,
'proxy_host' => '192.168.x.x',
'proxy_port' => 'xxxx',
'stream_context' => stream_context_create(
[
'ssl' => [
'proxy' => 'tcp://192.168.x.x:xxxx',
'request_fulluri' => true,
],
'http' => [
'proxy' => 'tcp://192.168.x.x:xxxx',
'request_fulluri' => true,
],
]
),
]);
This works as expected - all the traffic is going via the proxy server.
However, with the SoapServer class, I can't work out how to force it to send all outbound traffic via the SoapServer. It appears to be trying to load http://schemas.xmlsoap.org/soap/encoding/ directly form the network, not via the proxy, which is causing the "can't import schema from 'http://schemas.xmlsoap.org/soap/encoding/'" error to be thrown.
I've tried adding a hosts file entry for schemas.xmlsoap.org to 127.0.0.1 and hosting this file locally, but I'm still getting the same issue.
Is there something I'm missing?
Try stream_context_set_default like in file_get_contents:
file_get_contents behind a proxy?
<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234"; // Proxy server port
$PROXY_USER = "LOGIN"; // Username
$PROXY_PASS = "PASSWORD"; // Password
// Username and Password are required only if your proxy server needs basic authentication
$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
array(
'http' => array(
'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",
'request_fulluri' => true,
'header' => "Proxy-Authorization: Basic $auth"
// Remove the 'header' option if proxy authentication is not required
)
)
);
//Your SoapServer here
Or try to run server in non-WSDL mode
<?php
$server = new SoapServer(null, array('uri' => "http://localhost/namespace"));
$server->setClass('myClass');
$data = file_get_contents('php://input');
$server->handle($data);

Unable to connect to WSDL

I was working with older version of OpenSSL(OpenSSL 0.9.8o) and I was forced to use newer OpenSSL 1.0.1e-fips as the result I was unable to connect to WSDL:
Message: SoapClient::SoapClient(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
I need to disable SSL certification check, I tried:
$client = new SoapClient("https://IP:443/sdk/vimService?wsdl",
array(
"trace" => 1,
"location" => "https://IP:443/sdk/",
"stream_context" => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'allow_self_signed' => true,
)
)
)
)
);
`
And it throw:
Message: SoapClient::SoapClient(): Peer certificate CN=localhost.localdom' did not match expected CN=SAME IP AS IN SoapClient()'
Then I added 'peer_name'=> 'localhost.localdom', in stream_context and then it says XML file is empty:
Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document
PHP 5.5
Okey, I was able to found issue.
You can avoid this mess using stable PHP 5.5 version
Recently I learned that error: "looks like we got no XML document" is caused because of PHP version - PHP 5.6 in 5.5 working like a charm.
How to fix it in PHP 5.6
1) Remove SSL certificate check in PHP 5.6:
In 5.6 version SSL certification was enabled by default, so if you want to disabled it you must pass context stream:
"stream_context" => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
)
)
2) Deleted ?wsdl and added .wsdl instead (with ?wsdl, it didn't worke for me)
<?php
$client = new SoapClient("https://IP:443/sdk/vimService.wsdl",
array(
"trace" => 1,
"location" => "https://IP:443/sdk/",
'exceptions' => 1,
"stream_context" => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
)
)
)
);
$soapmsg["_this"] = array( "_" => "ServiceInstance", "type" => "ServiceInstance");
$result = $client->RetrieveServiceContent($soapmsg);
$ServiceContent = $result->returnval;
$soapmsg = NULL;
$soapmsg["_this"] = $ServiceContent->sessionManager;
$soapmsg["userName"] = "USERNAME";
$soapmsg["password"] = "PASSWORD";
$result = $client->Login($soapmsg);
$UserSession = $result->returnval;
echo "User, " . $UserSession->userName . ", successfully logged in!\n";
$soapmsg = NULL;
$soapmsg["_this"] = $ServiceContent->sessionManager;
$result = $client->Logout($soapmsg);
In my case stream_context_create didn't worked.
So I download this file here : https://curl.haxx.se/ca/cacert.pem
and placed it in my localhost as : F:\xampp\apache\cert.pem
and gave the same path for
openssl.cafile=F:\xampp\apache\cert.pem in my phpini
This made the localhost to acquire certificate and things worked great...!!
Posting this in case this may help some one going through my situation.
In my case was needed to add the crypt_method
"stream_context" => stream_context_create(
array(
'ssl' => array(
'verify_peer' => true,
'verify_peer_name' => true,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
)
)
)
In my case "stream_context" did the magic, i've just added the code :
`"stream_context" => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
)
)`

Https webservice php

Keep getting could not connect to host this is the server code that I have
<?php
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$soap_server = new SoapServer('Notification.wsdl');
$soap_server->addFunction('Notify');
function Notify($message)
{
return array('message' => $message);
}
$soap_server->handle();
?>
And this is my client code which is what is throwing the error. The endpoint is https. the wsdl file is local.
<?php
$context = stream_context_create(array(
'https' => array(
'cafile' => 'APX_WS_API1.cer',
'verify_peer' => true
)
));
$soap_client = new SoapClient('http://localhost:8888/Receiver/Notification.wsdl',
array('stream_context' => $context,'local_cer' => 'APX_WS_API1.cer'));
try{
$result = $soap_client->__soapCall('Notify',array('message' => 'Hello World'));
echo $result;
}catch(SoapFault $e){
echo "Error: " . $e->faultstring;
}
?>
Could someone please tell me what am i doing wrong. Thanks in advance
Is your certificate valid?
Try this option:
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'allow_self_signed' => true
)
));
If it works that means your cert is invalid.

Categories