简体   繁体   English

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

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

The following curl command is working fine.以下 curl 命令工作正常。

curl --header "Content-Type: application/soap+xml" --header "Accept: text/xml" --header "SOAPAction: ''" --data-binary @signedSoapRequest.xml http://server:8081/WebService curl --header "Content-Type: application/soap+xml" --header "Accept: text/xml" --header "SOAPAction: ''" --data-binary @signedSoapRequest.xml http://server:8081 /网络服务

I am trying to replicate same from PHP using curl as shown below.我正在尝试使用 curl 从 PHP 复制相同内容,如下所示。 But it fails with error code 403但它失败并出现错误代码 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));

If in $message I store a simple xml and not the signed xml data like below curls command.如果在$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 --header "Content-Type: application/soap+xml" --header "Accept: text/xml" --header "SOAPAction: ''" --data @SoapRequest.xml http://server:8081/WebService

For this curl coommand the PHP code works fine.对于这个 curl 命令,PHP 代码可以正常工作。 As this curl command use --data flag which means a text file/data and not binary/signed data.由于此 curl 命令使用 --data 标志,这意味着文本文件/数据而不是二进制/签名数据。

Any idea how it can work with signed xml also.知道它如何与签名的 xml 一起工作。 I tried setting content-type: applicaton/octet-stream but same 403 error.我尝试设置 content-type: applicaton/octet-stream 但同样的 403 错误。 (403)Forbidden (403)禁止

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

And it started working fine.它开始工作正常。

Instead of specifying value of file in $message variable and then setting $message in CURLOPT_POSTFIELDS, now using file_get_contents() to read file and set CURLOPT_POSTFIELDS.而不是在 $message 变量中指定文件的值,然后在 CURLOPT_POSTFIELDS 中设置 $message,现在使用 file_get_contents() 来读取文件并设置 CURLOPT_POSTFIELDS。

Taken hint from PHP cURL: how to set body to binary data?PHP cURL 得到提示:如何将正文设置为二进制数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM