在 PHP 如何使用 curl 将已签名的 xml 作为 soap 请求发送

In PHP how to send signed xml as soap request using curl

以下 curl 命令运行良好。

curl --header "Content-Type: application/soap+xml" --header "Accept: text/xml" --header "SOAPAction: ''" --data -二进制@signedSoapRequest.xml http://server:8081/WebService

我正在尝试使用 curl 从 PHP 复制相同内容,如下所示。但它失败了,错误代码为 403

    $message = <?xml version="1.0"....signed xml data...</SOAP-ENV:Envelope>
    $headers = array(
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: ''",
        "Content-length: ".strlen($message),
    );

    $result = array();

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_VERBOSE, false);
    curl_setopt($ch, CURLOPT_URL, $this->endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
    $result['soap_result'] = $this->cleanXML(curl_exec($ch));

如果在 $message 中,我存储了一个简单的 xml 而不是签名的 xml 数据,如下面的 curls 命令。 curl --header "Content-Type: application/soap+xml" --header "Accept: text/xml" --header "SOAPAction: ''" --data @SoapRequest.xml http://server:8081/WebService

对于这个 curl 命令,PHP 代码工作正常。 由于此 curl 命令使用 --data 标志,这意味着文本 file/data 而不是 binary/signed 数据。

知道它如何与已签名的 xml 一起工作。我尝试设置 content-type: applicaton/octet-stream 但同样的 403 错误。 (403)禁止

代码 curl_setopt($ch, CURLOPT_POSTFIELDS, $message); 替换为 curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('soapRequest.xml'))

它开始正常工作。

不再在 $message 变量中指定文件的值,然后在 CURLOPT_POSTFIELDS 中设置 $message,现在使用 file_get_contents() 读取文件并设置 CURLOPT_POSTFIELDS.

得到提示