php soapClient证书调用https开头的webservice失败有关问题解决方案
php soapClient证书调用https开头的webservice失败问题解决方案
这几天做了服务器的php版本升级,由5.4升级到5.6.19,其他部分都没问题,结果发现https的webservice证书调用失败,报:failed to load external entity错误,代码都没动过,初步判断升级后导致的,于是本地还原到5.4 果然又好使了。
以前调用代码为:
$params = array('id' => '2'); $local_cert = "./client-cer.pem"; set_time_limit(0); try{ //ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache $wsdl='https://192.168.1.146:8443/pro/ws/getInfoService?wsdl'; // echo file_get_contents($wsdl); $soap=new SoapClient($wsdl, array( 'trace'=>true, 'cache_wsdl'=>WSDL_CACHE_NONE, 'soap_version' => SOAP_1_1, 'local_cert' => $local_cert, //client证书信息 'passphrase'=> 'client', //密码 // 'allow_self_signed'=> true ) ); $result=$soap->sayHello($params); $result_json= json_encode($result); $result= json_decode($result_json,true); echo '结果为:' . json_decode($result['return'],true); }catch(Exception $e) { $result['success'] = '0'; $result['msg'] = '请求超时'; echo $e->getMessage(); } echo '>>>>>>>>>>>';
根据官方解释如下:
After migrating to PHP 5.6.5, the soap 1.2 did not work anymore. So I solved the problem by adding optional parameters SSL. My error: failed to load external entity How to solve: // options for ssl in php 5.6.5 $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) ); $oSoapClient = new SoapClient ( $url . "?WSDL", $params );
所以更改代码为:
$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_1, 'trace' => 1, 'exceptions' => 1, "connection_timeout" => 180, 'stream_context' => stream_context_create ( $opts ), 'local_cert' => $local_cert, // client证书信息 'passphrase' => 'client' // 密码 ); ; $soap = new SoapClient ( $wsdl, $params );
开始按这个做也报错,后来把soap_1_2更改为soap_1_1 好使了