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

标签 php soap curl https

我正在尝试通过 https 对用 C# 编写的网络服务执行 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>"

地点:

Ошибка при проверке безопасности сообщения.

意思是这样的:

Communication security check error.

我尝试了什么:

  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?

还有更多。

问题:我做错了什么?

问候。

附言:已使用 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 答案。

你应该看看服务器端。

关于php - 通过 cURL 使用自签名证书通过 https 执行 soap 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18094113/

相关文章:

java - JAX-WS服务部署

javascript - Php 值在外部 jquery 脚本中不起作用

php - 如何在 PHP 7.0 Ubuntu 18.10 中启用 SoapClient

php - PHP 中的空 MySQL 查询结果

java - 从 EncryptedKey 获取 SecretKey 时出错(JRE7 到 JRE8 升级)

api - 无法通过 API 将测试运行状态更新为已完成

curl - Flume/Elasticsearch创建新索引并忽略显式创建的索引

curl - 如何在 Ubuntu 上使用旧版 SSL 支持编译 Curl?

javascript - <select> 选项无需 AJAX 即可更改

php - 如何在 PHP 中清除以前回显的项目