Https webservice php - 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.

Related

How can I use SoapClient in my Php Programm

How can I use the SoapClient with the method "sites_web_domain_add" to interact with the ispconfig api? My Code produces a folowing error:
error
Also soap client/server is set to enabled.
If i execute the code in my terminal it just works fine but not in my controller, so have I missed something? Like importing the right soap?
Thanks for helping!
$username = 'apiuser';
$password = '1234';
$soap_location = 'https://localhost:8080/remote/index.php';
$soap_uri = 'https://localhost:8080/remote/';
$context = stream_context_create(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
]
);
$client = new SoapClient(
null,
[
'location' => $soap_location,
'uri' => $soap_uri,
'trace' => 1,
'exceptions' => 1,
'stream_context' => $context
]
);
try {
if ($session_id = $client->login($username, $password)) {
echo "Login successful. Session ID: $session_id<br>";
}
//* Set the function parameters.
$client_id = 1;
$params = [
'server_id' => 1,
'ip_address' => '*',
'domain' => 'test2.int',
'type' => 'vhost', // vhost | alias | vhostalias | subdomain | vhostsubdomain
];
$client->sites_web_domain_add($session_id, $client_id, $params, $readonly = false);
if ($client->logout($session_id)) {
echo "Logged out.<br>";
}
} catch (SoapFault $e) {
echo $client->__getLastResponse();
die("SOAP Error: {$e->getMessage()}");
}

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 :)

SoapFault exception: [HTTP] "Could not connect"

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);
}
?>

https for stream_context_create

Iam new to trying like this, I have trying to get the token with https in magento2, it not working here my code
<?php
$base_url="https://myurl.com/";
$domain="myurl.com";
$url = $base_url.'index.php/rest/V1/integration/admin/token';
$body = '{"username":"ApiUser", "password":"ApiPass"}';
$context_options = array (
'https' => array (
'method' => 'POST',
'header'=> "Content-type: application/json"
'content' => $body
),
'ssl' => array('SNI_enabled'=>true,'SNI_server_name'=> $domain)
);
$context = stream_context_create($context_options);
$token = json_decode(file_get_contents($url, false, $context));
echo $token; echo "<br/>";
?>
Please help, thanks in advance.
Sorry for the super late reply, but was just looking for this solution as well, to use https URL instead of http.
FROM the PHP manual page/example, the context should not be https, but rather http.:
http://php.net/manual/it/function.stream-context-create.php#74795
INCORRECT:
$context_options = array (
'https' => array ()
)
CORRECT:
$context_options = array (
'http' => array ()
)
of if you need more SSL options, this comment:
http://php.net/manual/it/function.stream-context-create.php#110158
$contextOptions = array(
'ssl' => array(
'verify_peer' => true,
'cafile' => __DIR__ . '/cacert.pem',
'verify_depth' => 5,
'CN_match' => 'secure.example.com'
)
);
The simplest way to create stream context with stream_context_create when working in SSL environment is to omit verification! This works just fine for me when I want to retrieve data with file_get_contents:
stream_context_create([
'http' => [ /* your options here - eg: */ 'method' => 'POST', ],
// 'https' => 'DON'T forget there is no "https", only "http" like above',
'ssl' => [ // here comes the actual SSL part...
'verify_peer' => false,
'verify_peer_name' => false,
]
]);
php.net/manual/en/context.ssl.php:
verify_peer boolean
Require verification of SSL certificate used.
Defaults to TRUE.
verify_peer_name boolean
Require verification of peer name.
Defaults to TRUE.

PHP SOAP WSDL - Could not connect to host

there is a problem trying to execute some functions from the WSDL I have. I connected to the WSDL using Basic Auth, I can see all the functions available with:$functions = $client->__getFunctions();
But then I try to execute any of them I get "[HTTP] Could not connect to host" error. My code here:
ini_set('default_socket_timeout', 150);
header('Content-Type: text/plain');
ini_set("soap.wsdl_cache_enabled", "0");
$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),
'login' => 'login',
'password' => 'password',
'cache_wsdl' => WSDL_CACHE_NONE
);
$url = "http://address/webservice/wsdl";
try {
$client = new SoapClient($url, $params);
$functions = $client->__getFunctions();
var_dump($functions);
$response = $client->__soapCall('function_name', array());
$client->function_name();
var_dump($response);
} catch (SoapFault $fault) {
echo '<br>'.$fault;
}
Any ideas? Now in the WSDL file I have a targetNamespace parameter which is "targetNamespace="http://192.168.0.253:85/webservice/soap"" can this be a legit WSDL file? I mean can the namespace be a localhost ip address? Maybe this needs to be fixed in the WSDL side?
Found the solution. What was missing:
'location' => "http://address/webservice/soap",
In parameters.

Categories