使用cURL的自签名证书通过https执行soap请求


Perform soap request via https with self-signed certificate by cURL

我正在尝试通过https对在C#上编写的web服务执行soap请求。服务器使用自签名证书。

在多次尝试使用SoapClient失败后,我决定使用纯cURL来执行请求。

我的代码:

<?php
header('Content-Type: text/plain; charset=utf-8');
$url      = 'https://ip:8443/ServiceName';
$admin    = 'login';
$password = 'haShedPassw0rdHere';
$post     =
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        // Soap service params xml
    </soap:Body>
</soap:Envelope>';
$headers = array(             
    'Content-type: text/xml;charset="utf-8"',
    'Accept: text/xml', 
    'Cache-Control: no-cache', 
    'Pragma: no-cache', 
    'SOAPAction: https://ip:8443/ServiceName',
    'Content-length: ' . strlen($post),
);
$curl = curl_init();
$options = array(
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_HTTPAUTH => CURLAUTH_ANY,
    CURLOPT_USERPWD => $admin . ':' . $password,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $post,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => false,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING       => '',
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120,
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10
);
curl_setopt_array($curl, $options);
var_dump($response = curl_exec($curl));
?>

响应:

string(370) "
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                a:InvalidSecurity
            </faultcode>
            <faultstring xml:lang="ru-RU">
                Ошибка при проверке безопасности сообщения.
            </faultstring>
        </s:Fault>
    </s:Body>
</s:Envelope>"

其中:

О刺绣。

意思是:

通信安全检查错误。

我试过什么:

  1. 带有自签名证书的POST请求
  2. 如何使用带有PHP页面的自定义用户名验证的WCF Web服务
  3. Php SoapClient stream_context选项
  4. 使用PHP的SOAP身份验证
  5. 如何通过Curl和PHP发送SOAP XML

还有更多。

问题:我做错了什么?

谨致问候。

p.S.:PHP 5.3PHP 5.4.14PHP 5.5.1进行测试。结果是一样的。


UPDv1:

C#来源,由服务支持团队提供:

private void Button_SetData_Click(object sender, EventArgs e)
{
    eLeed.WebServiceClient client =
        new eLeed.WebServiceClient();
    client.ClientCredentials.UserName.UserName = "login";
    client.ClientCredentials.UserName.Password = "haShedPassw0rdHere";
    Stream input = null;
    input = GetQuery("ServiceMethod", TextBox_Command.Text);
    XmlDocument response = new XmlDocument();
    response.PreserveWhitespace = true;
    response.Load(client.SetDataContractor(input));
    ExeResponse(response);
    input.Close();
    client.Close();
}

这个按钮确实在工作。但是如何在php中用cURL执行类似的操作呢?特别是,如何通过这两条线:

client.ClientCredentials.UserName.UserName = "login";
client.ClientCredentials.UserName.Password = "haShedPassw0rdHere";

所以,它不应该返回类似"无效凭据"之类的消息吗?

错误似乎不是在客户端,而是在服务器端。服务器表示某些安全检查失败。如果这是一个客户端错误,您将只得到cURL的错误。您将得到一个XML答案。

你应该看看服务器端。