我正在尝试通过 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.
我尝试了什么:
- POST request with a self-signed certificate
- How to consume a WCF Web Service that uses custom username validation with a PHP page?
- Php SoapClient stream_context option
- SOAP authentication with PHP
- How can I send SOAP XML via Curl and PHP?
还有更多。
问题:我做错了什么?
问候。
附言:已使用 PHP 5.3
、PHP 5.4.14
、PHP 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/