使用php:SoapClient向https soap服务器执行soap请求时失败,并出现SoapFault:
faultstring:无法连接到主机错误代码:HTTP
我必须确保soap服务器ssl证书是有效的,所以我使用
<?php
$soap=new SoapClient("mwsdl.wsdl"
,array(
"location"=>"https:examplesoapserver.com"
,"trace"=>1
,"stream_context"=>stream_context_create(array(
"ssl"=>array(
"verify_peer"=>true
,"allow_self_signed"=>false
)
)
)
)
);
但这给了我SoapFault。
我测试了不同的设置:
OK位置:具有自签名证书verify_peer=>trueallow_self_signed=>true
OK(无需设置verify_peer)位置:具有自签名证书allow_self_signed=>false
NOT OK位置:具有自签名证书verify_peer=>trueallow_self_signed=>false
OK(无需设置verify_peer)位置:ca签名证书allow_self_signed=>false
NOT OK位置:ca签名证书verify_peer=>trueallow_self_signed=>false
我有:
php版本:5.3.3CentOS 6.4Apache/2.2.15
询问更多细节是否有助于解决问题。
提前感谢您的帮助!
问题是php默认情况下有不安全的设置来处理https请求。它不会验证证书或检查证书中的域是否正确。
您还必须下载用于自己验证证书的文件。
您可以在以下位置下载:http://curl.haxx.se/ca/cacert.pem
正确处理它们的设置是
<?php
$client = new SoapClient("my-wsdlfile.wsdl"
,array(
"location"=>"https://examplesoapserver.com"
,"stream_context"=>stream_context_create(
array(
"ssl"=>array(
"verify_peer"=>true
,"allow_self_signed"=>false
,"cafile"=>"path/to/cacert.pem"
,"verify_depth"=>5
,"CN_match"=>"examplesoapserver.com"
)
)
)
)
);
其他可以使用https(如file_get_contents
)的php函数也存在同样的问题,因此同样的安全解决方案也应该有效。
CURL默认情况下有安全设置,因此如果可能的话,它更容易使用。
以下是php如何处理https:的更详细的解释
http://phpsecurity.readthedocs.org/en/latest/Transport-Layer-Security-(HTTPS SSL和TLS).html#php流