无法在soapclient和https中发送请求
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在soapclient和https中发送请求相关的知识,希望对你有一定的参考价值。
我的例程在http协议下工作正常,但最近我需要升级到https并且不再工作。我不知道发生了什么,我在网上尝试了很多不同的代码。
错误消息是Uncaught SoapFault异常:[HTTP]无法连接到主机[...]
我正在使用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');
以上是关于无法在soapclient和https中发送请求的主要内容,如果未能解决你的问题,请参考以下文章
在发送请求之前/不发送请求之前检查由 PHP SoapClient 调用创建的 XML
SoapClient无法从SimpleXMLObject渲染正确的xml
使用php soapclient工具包显示访问netsuite wsdl无法加载wsdl
PHP SoapClient over selfsigned certificate,SOAP-ERROR:解析WSDL:无法从'https://加载