无法在 soapclient 和 https 中发送请求
Can't send request in soapclient and https
我的例程在 http 协议下工作正常,但最近我需要升级到 https 并且不再工作。我不知道发生了什么,我在网上尝试了很多不同的代码。
错误消息是 Uncaught SoapFault exception: [HTTP] Could not connect to host [...]
我使用的是 php v.5.6,但如果此解决方案也适用于 5.3.37 则更好。
这是我的代码,感谢您的帮助
<?php
error_reporting(E_ERROR | E_PARSE);
ini_set('display_errors', 1);
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
ini_set('default_socket_timeout', 1000);
$url_wsdl ="https://exampleservice.cls?WSDL=1&CacheUserName=myusername&CachePassword=mypassword&CacheNoRedirect=1";
print 'Starting routine';
//logging in
$curl = curl_init($url_wsdl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
// get cookies
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
curl_close($curl);
print 'Curl....ok. Cookies ok<br>';
$options = array(
'trace' => 1
, 'cache_wsdl' => WSDL_CACHE_NONE
, 'user_agent' => 'PHP WS'
, 'ssl_method' => SOAP_SSL_METHOD_SSLv3
);
$soapClient = new SoapClient($url_wsdl,$options );
print 'SoapClient....ok<br>';
//sending cookies to soapclient
foreach ($cookies as $name => $value) {
print '------> sending cookies: '. $value . '<br>';
$soapClient->__setCookie($name, $value);
}
print 'SetCookies....ok<br>';
$data = array('TesteInterface' => array('Request' => array('Mensagem' => 'teste')));
$response = $soapClient->__soapCall("TesteInterface",$data);
print 'SoapCall....ok<br>';
print 'Response:<br>';
print_r($response);
?>
首先检查您的服务器是否有 openssl
库,然后检查要激活的 openssl php 扩展。
之后你可以改进你的wsdl $options
:
$options = [
'trace' => 1,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'soap_version' => SOAP_1_2,
'encoding' => 'UTF-8',
'connection_timeout' => 60, //or other
'keep_alive' => false,
'ssl_method' => SOAP_SSL_METHOD_TLS, //or other
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]),
];
我已经通过手动设置端点解决了这个问题。它指向一个没有 https 的地址。现在可以使用了
$soapClient->__setLocation('https://myservice.cls');
我的例程在 http 协议下工作正常,但最近我需要升级到 https 并且不再工作。我不知道发生了什么,我在网上尝试了很多不同的代码。
错误消息是 Uncaught SoapFault exception: [HTTP] Could not connect to host [...]
我使用的是 php v.5.6,但如果此解决方案也适用于 5.3.37 则更好。
这是我的代码,感谢您的帮助
<?php
error_reporting(E_ERROR | E_PARSE);
ini_set('display_errors', 1);
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
ini_set('default_socket_timeout', 1000);
$url_wsdl ="https://exampleservice.cls?WSDL=1&CacheUserName=myusername&CachePassword=mypassword&CacheNoRedirect=1";
print 'Starting routine';
//logging in
$curl = curl_init($url_wsdl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
// get cookies
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
curl_close($curl);
print 'Curl....ok. Cookies ok<br>';
$options = array(
'trace' => 1
, 'cache_wsdl' => WSDL_CACHE_NONE
, 'user_agent' => 'PHP WS'
, 'ssl_method' => SOAP_SSL_METHOD_SSLv3
);
$soapClient = new SoapClient($url_wsdl,$options );
print 'SoapClient....ok<br>';
//sending cookies to soapclient
foreach ($cookies as $name => $value) {
print '------> sending cookies: '. $value . '<br>';
$soapClient->__setCookie($name, $value);
}
print 'SetCookies....ok<br>';
$data = array('TesteInterface' => array('Request' => array('Mensagem' => 'teste')));
$response = $soapClient->__soapCall("TesteInterface",$data);
print 'SoapCall....ok<br>';
print 'Response:<br>';
print_r($response);
?>
首先检查您的服务器是否有 openssl
库,然后检查要激活的 openssl php 扩展。
之后你可以改进你的wsdl $options
:
$options = [
'trace' => 1,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'soap_version' => SOAP_1_2,
'encoding' => 'UTF-8',
'connection_timeout' => 60, //or other
'keep_alive' => false,
'ssl_method' => SOAP_SSL_METHOD_TLS, //or other
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]),
];
我已经通过手动设置端点解决了这个问题。它指向一个没有 https 的地址。现在可以使用了
$soapClient->__setLocation('https://myservice.cls');