PHP SOAP WSDL - Could not connect to host - php

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.

Related

PHP SoapClient Cannot process the message because the content type 'text/xml;

I cannot connect to webservice and send/receive data
Error
HTTP,Cannot process the message because the content type 'text/xml;
charset=utf-8' was not the expected type 'application/soap+xml;
charset=utf-8'.
Code
$parameters = [
'UserName' => 12324,
'Password' => 432123,
'Bill_Id' => 153585611140,
'Payment_Id' => 8560103,
];
$url="https://bill.samanepay.com/CheckBill/BillStateService.svc?wsdl";
$method = "VerifyBillPaymentWithAddData";
$client = new SoapClient($url);
try{
$info = $client->__call($method, array($parameters));
}catch (SoapFault $fault){
die($fault->faultcode.','.$fault->faultstring);
}
Notice : not work Soap version 1,1 and other resolve sample for this error in stackoverflow.
You could try
$url = "https://bill.samanepay.com/CheckBill/BillStateService.svc?wsdl";
try {
$client = new SoapClient($url, [
"soap_version" => SOAP_1_2, // SOAP_1_1
'cache_wsdl' => WSDL_CACHE_NONE, // WSDL_CACHE_MEMORY
'trace' => 1,
'exception' => 1,
'keep_alive' => false,
'connection_timeout' => 500000
]);
print_r($client->__getFunctions());
} catch (SOAPFault $f) {
error_log('ERROR => '.$f);
}
to verify that your method name is correct.
There you can see the method
VerifyBillPaymentWithAddDataResponse VerifyBillPaymentWithAddData(VerifyBillPaymentWithAddData $parameters)
Next is to check the Type VerifyBillPaymentWithAddData and if the parameter can be an array.
Also you could test to call the method via
$client->VerifyBillPaymentWithAddData([
'UserName' => 12324,
'Password' => 432123,
'Bill_Id' => 153585611140,
'Payment_Id' => 8560103,
]);
or yours except the additional array
$info = $client->__call($method, $parameters);
EDIT:
Assuming to https://stackoverflow.com/a/5409465/1152471 the error could be on the server side, because the server sends an header back that is not compatible with SOAP 1.2 standard.
Maybe you have to use an third party library or even simple sockets to get it working.
Just use the following function. Have fun!
function WebServices($function, $parameters){
$username = '***';
$password = '***';
$url = "http://*.*.*.*/*/*/*WebService.svc?wsdl";
$service_url = 'http://*.*.*.*/*/*/*WebService.svc';
$client = new SoapClient($url, [
"soap_version" => SOAP_1_2,
"UserName"=>$username,
"Password"=>$password,
"SOAPAction"=>"http://tempuri.org/I*WebService/$function",
'cache_wsdl' => WSDL_CACHE_NONE, // WSDL_CACHE_MEMORY
'trace' => 1,
'exception' => 1,
'keep_alive' => false,
'connection_timeout' => 500000
]);
$action = new \SoapHeader('http://www.w3.org/2005/08/addressing', 'Action', "http://tempuri.org/I*WebService/$function");
$to = new \SoapHeader('http://www.w3.org/2005/08/addressing', 'To', $service_url);
$client->__setSoapHeaders([$action, $to]);
try{
return $client->__call($function, $parameters);
} catch(SoapFault $e){
return $e->getMessage();
}
}

SOAP-ERROR: Parsing WSDL: Couldn't load from (Working on SOAPUI bu not on my Ubuntu)

I am facing a weird problem. I am trying to consume a specific webservice, which you can see on:
https://api-hom.amil.com.br/operadora/tiss/LoteGuias/soap/v30301?wsdl
Through the soap client I can consume and receive result properly.
But, when I am consuming through my PHP code I got "SOAP-ERROR: Parsing Schema: can't import schema from 'https://api-hom.amil.com.br/ssg/wsdl/tissWebServicesV3_03_01.xsd?serviceoid=477f074ec66fee8e74f533c102c312ff&servdocoid=477f074ec66fee8e74f533c102c31348'" error
My code is below:
public function webservice_request($soap_request, $partner) {
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
'trace' => 1,
'soap_version' => SOAP_1_1,
'exceptions'=> true,
'encoding' => 'UTF-8',
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_DEFLATE,
'cache_wsdl' => WSDL_CACHE_NONE
)
));
// Transformando em Array
$json = \GuzzleHttp\json_encode($soap_request);
$xml = \GuzzleHttp\json_decode($json);
$client = null;
try
{
$client = new \SoapClient($partner->webservice_url, array('trace' => true, 'stream_context' => $context));
$result = $client->__call('tissLoteGuias_Operation', array($xml));
return $this->check_response($result, 'NORMAL', $client);
} catch (\Exception $e) {
if (isset($client) == false) {
return $this->check_response($e, 'WSDL_ERROR', null);
} else {
return $this->check_response($e, 'FAULT', $client);
}
}
}
Observations:
[1] The problem happens on line:
$client = new \SoapClient($partner->webservice_url, array('trace' => true, 'stream_context' => $context));
[2] when I execute the same code on my Macbook it works, but in my Ubuntu server and Ubuntu Desktop it isn't working.
What I am doing wrong?

I am able to load an wsdl file through curl .but not through SoapClient. I am not sure why?

getting the below soap error
"SOAP-ERROR: Parsing WSDL: Couldn't load from
'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSource
Transaction_1.25.wsdl' : failed to load external entity
"https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.25.wsdl
This is the code:
<?php
$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) );
$url = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.26.wsdl";
try{
$client = new SoapClient($url,$params );
var_dump($client);
}
catch(SoapFault $fault) {
echo '<br>'.$fault;
}
?>
I used this code to list functions:
$url = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.26.wsdl";
$client = new SoapClient($url);
var_dump($client->__getFunctions());
and this gives one function:
array(1) {
[0]=>
string(50) "ReplyMessage runTransaction(RequestMessage $input)"
}

PHP and Royal Mail Tracking API

We have been having some issues with setting up a PHP SOAP Client to make use of the Royal Mail Tracking API. We have an account all set up with Royal Mail and have our ID's and secrets. We can get it to work using SOAPUI but we are always getting a "Wrong Version" error when trying to implement it in PHP. We have the WSDL file locally (which Royal Mail provided via their developer portal) this works with SOAPUI but not PHP SOAP Client. We were hoping someone may be able to see if we are doing anything wrong. I'll post the code below but will omit our secret and ID from the code.
<?php
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 900);
ini_set('default_socket_timeout', 15);
$trackingNumber = 'F111111111JD';
$time = gmdate('Y-m-d\TH:i:s');
$intHeaders = [
'dateTime' => $time,
'version' => '1.0',
'identification' => [
'applicationId' => '***********',
'transactionId' => 123456
]
];
$wsdl = 'WSDL/Tracking_API_V1_1_1.wsdl';
$options = array(
'uri'=>'http://schemas.xmlsoap.org/soap/envelope/',
'style'=>SOAP_RPC,
'use'=>SOAP_ENCODED,
'soap_version'=>SOAP_1_2,
'cache_wsdl'=>WSDL_CACHE_NONE,
'connection_timeout'=>15,
'trace'=>true,
'encoding'=>'UTF-8',
'exceptions'=>true,
'stream_context' => stream_context_create([
"http" => [
'Accept' => 'application/soap+xml',
'X-IBM-Client-Secret' => '****',
'X-IBM-Client-Id'=> '****'
]
])
);
try {
$soap = new SoapClient($wsdl, $options);
$data = $soap->getSingleItemHistory(['integrationHeader' => $intHeaders, 'trackingNumber' => $trackingNumber]);
}
catch(Exception $e) {
die($e->getMessage());
}
var_dump($data);
die;
We've tried using SOAP_1_1 and SOAP_1_2 for the 'soap_version' but it always comes back with the "Wrong Version" error.
Hope someone is able to help.
You need to set the header key inside the http array like so:
'stream_context' => stream_context_create(
[
'http' =>
[
'header' => implode(
"\r\n",
[
'Accept: application/soap+xml',
'X-IBM-Client-Id: ' . $clientId,
'X-IBM-Client-Secret: ' . $clientSecret,
]
),
],
]
)

PHP SOAP Could not connect to host on function execution

I'm trying to achieve a PHP SOAP client to a HTTPS service and am encountering a problem I'm not able to resolve. Am I missing something ?
I'm establishing a connection to the webservice as follow :
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'allow_self_signed' => true
)
));
$opt = array(
"login" => self::LOGIN,
"password" => self::PASSWORD,
"authentication" => SOAP_AUTHENTICATION_BASIC,
"trace" => true,
"exceptions" => 1,
"cache_wsdl" => WSDL_CACHE_NONE,
'stream_context' => $context,
"connection_timeout" => 30
);
try {
$soapClient = new SoapClient(self::WS_URL, $opt);
} catch (SoapFault $fault) {
var_dump($fault);
exit;
}
Until there, no problem seems to arise. Then I do the following :
var_dump($soapClient->__getFunctions());
This gives me the list of functions the service can process and I get a valid response :
array(1) { [0]=> string(54) "ListReponseAX getElig(Elig $in0)" }
The problem then arises : when I try to invoke the getElig function, no matter how I try, I get this "Could not connect to host" error.
I've tried passing the data as text, as array, as object, as soapvar but always get this annoying error.
Thx in advance for any help !
Ok the problem is resolved. For those wondering, the WSDL file had an endpoint specification in it that was irrelevant. By forcing the location upon initializing the PHP class, I was able to circumvent the problem :
$opt = array(
"login" => self::LOGIN,
"password" => self::PASSWORD,
"authentication" => SOAP_AUTHENTICATION_BASIC,
"trace" => true,
"exceptions" => 1,
"cache_wsdl" => WSDL_CACHE_NONE,
'stream_context' => $context,
"connection_timeout" => 30,
"location" => "https://the_location_to_force"
);

Categories