Php SoapClient stream_context 选项

标签 php soap ssl https

我想使用第三方的网络服务。要使用 Web 服务,我需要使用 HTTPS 连接。我的问题是,在开发过程中,我有一个带有无效证书的测试 api。我想将 SoapClient 设置为 no 来验证服务器的证书。这是我尝试过的方法:

$opts = array(
    'ssl'   => array(
            'verify_peer'          => false
        ),
    'https' => array(
            'curl_verify_ssl_peer'  => false,
            'curl_verify_ssl_host'  => false
     )
);
$streamContext = stream_context_create($opts);
$client = new SoapClient("https://urlToSoapWs",
  array(
      'login'             => 'user',
      'password'          => 'pwd',
      'authentication'    => SOAP_AUTHENTICATION_BASIC,
      'local_cert'        => file_get_contents('C:/somelocation/1.pem'),
      'passphrase'        => 'passphrase',
      'stream_context'    => $streamContext
  ));

我也尝试过使用 CURL 并成功了!但我想使用 SoapClient。您可以在下面找到带有 CURL 的代码:

// create a new cURL resource
$ch = curl_init("https://urlToSoapWs");

// setting the request type to POST: 
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); 
// setting the authorization method to BASIC: 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
// supplying your credentials: 
curl_setopt($ch, CURLOPT_USERPWD, "user:pwd");

$body = "<SOAP-ENV:Envelope>somexmlhere</SOAP-ENV:Envelope>";
// filling the request body with your SOAP message: 
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// configuring cURL not to verify the server certificate: 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLCERT, "pathToTheCertificatePemFile");
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "pwd");
//curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");

curl_setopt($ch, CURLOPT_SSLKEY, "pathTotheKeyFile");
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, "pwd");

// telling cURL to return the HTTP response body as operation result 
// value when calling curl_exec: 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// calling cURL and saving the SOAP response message in a variable which 
// contains a string like "<SOAP-ENV:Envelope ...>...</SOAP-ENV:Envelope>": 


$result = curl_exec($ch);
// closing cURL: 
curl_close($ch);

如果您在我使用 SoapClient 提供的代码中发现了错误,请发布它。 谢谢。

最佳答案

也许问题不是无效证书,而是 SSLv2/3 握手问题;您可以尝试手动指定一个密码,如下所示:

$stream_opts = array(
//      'ssl'=>array('ciphers'=>"3DES" // also working
//      further ciphers on http://www.openssl.org/docs/apps/ciphers.html
        'ssl'=>array('ciphers'=>"SHA1"
      )
);

$myStreamContext = stream_context_create($stream_opts);
$soapOptions['stream_context'] = $stream_opts;
$soapClient = new SoapAuthClient("https://...", $soapOptions);

祝你好运!

关于Php SoapClient stream_context 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9909232/

相关文章:

php - 左连接VS内连接较少行数mysql pdo php

php - PHP中没有错误信息

web-services - Delphi 2007 生成不正确的 SOAP 消息

php - 如何设置 OpenSSL 以便我可以使用 PHP vis Gmail SMTP 发送邮件?

java - SSL 握手异常 - RESTEASY004655

iOS iPhone iPad 限制 SSL/HTTPS 加密位

php - 使用 MySQL 的 AES_ECRYPT 函数加密一个值,然后使用 PHP 在 URL 字符串中传递它

php - 使用 zend view helper 的下拉菜单

java - 用于 Web 服务客户端的 AXIS 与 JAX-WS

java - 如何将 SOAP 方法转换为 Restful API?