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

时间:2013-08-07 03:25:15

标签: php soap curl https

我正在尝试通过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 request with a self-signed certificate
  2. How to consume a WCF Web Service that uses custom username validation with a PHP page?
  3. Php SoapClient stream_context option
  4. SOAP authentication with PHP
  5. How can I send SOAP XML via Curl and PHP?
  6. 其中更多。

    问题:我做错了什么?

    问候。

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


    UPDv1:

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

    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();
    }
    

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

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

    因此,它不应该返回“无效凭据”之类的消息吗?

1 个答案:

答案 0 :(得分:1)

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

你应该看一下服务器端。