使用标准 xml 信封和 pdf 附件对 soap 服务的 CURL 请求

CURL request to soap service with standard xml envelope and pdf attachment

几天来,我一直在拼命尝试使用一些参数和一个包含 PDF 的二进制 MIME 附件来调用 soap 服务。

我正在使用基于 Apache 和 PHP7 的基础架构。 我最初尝试使用本机 php soapclient class 但我知道 class 不允许管理附件。然后我开始用 php curl 复制在 Soap-UI 上测试的调用。

在我看来调用的内容是相同的,但我继续收到 soap 错误。

这是呼叫请求设置:

$file = "./CI_test.pdf";
$fileContents = file_get_contents($file);

$url = "https://xxxxxxxx/JProtocolloDocArea/services/DOCAREAProtoSoap";

$boundary = bin2hex ( random_bytes ( 10 ) );


$soap_request  = "";

$soap_request .= "\r\n";
$soap_request .= "\r\n";
$soap_request .= "------=_Part_".$boundary;
$soap_request .= "\r\n";
$soap_request .= "Content-Type: text/xml; charset=UTF-8";
$soap_request .= "\r\n";
$soap_request .= "Content-Transfer-Encoding: 8bit";
$soap_request .= "\r\n";
$soap_request .= "Content-ID: <rootpart@soapui.org>";
$soap_request .= "\r\n";
$soap_request .= "\r\n";
$soap_request .= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n";
$soap_request .= "   <soapenv:Header/>\n";
$soap_request .= "   <soapenv:Body>\n";
$soap_request .= "      <tem:inserimento>\n";
$soap_request .= "         <strUserName>ESU_PD</strUserName>\n";
$soap_request .= "         <strDST>ebFfEeBKYk9yo4jR1M1V8VdSu3VrDpLqK28OzrxiUJvYH83YY4fmAgWUgjmWF2Cb65WTPzN76w5YnKK3OlSss9bva5j29125</strDST>\n";
$soap_request .= "      </tem:inserimento>\n";
$soap_request .= "   </soapenv:Body>\n";
$soap_request .= "</soapenv:Envelope>";
$soap_request .= "\r\n";
$soap_request .= "------=_Part_".$boundary;
$soap_request .= "\r\n";
$soap_request .= "Content-Type: application/pdf; name=CI_test.pdf";
$soap_request .= "\r\n";
$soap_request .= "Content-Transfer-Encoding: binary";
$soap_request .= "\r\n";
$soap_request .= "Content-ID: <CI_test.pdf>";
$soap_request .= "\r\n";
$soap_request .= "Content-Disposition: attachment; name=\"CI_test.pdf\"; filename=\"CI_test.pdf\"";
$soap_request .= "\r\n";
$soap_request .= "\r\n";
$soap_request .= $fileContents;
$soap_request .= "\r\n";
$soap_request .= "------=_Part_".$boundary."--";
$soap_request .= "\r\n";

这是 header 设置:

$header = array(
    "Accept-Encoding: gzip,deflate",
    "Host: esupd-test.e-pal.it",
    "MIME-Version: 1.0",
    "Content-Type: multipart/related; type=\"text/xml\"; start=\"<rootpart@soapui.org>\"; boundary=\"----=_Part_$boundary\"",
    "SOAPAction: \"\"",
    "Content-Length: ".strlen($soap_request)."",
    "Connection: Keep-Alive",
    "Cache-Control: no-cache",
    "Pragma: no-cache"
);

这就是 CURL 调用:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST,           true );
curl_setopt($ch, CURLOPT_POSTFIELDS,     $soap_request);
curl_setopt($ch, CURLOPT_HTTPHEADER,     $header);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); // capture the header info
curl_setopt($ch, CURLOPT_VERBOSE, 1); // turn verbose on

$result = curl_exec($ch);

如有任何建议,我们将不胜感激。

问题算是解决了

由于接收调用的服务器出现问题,请求失败。

因此,我报告的 PHP 脚本可能是调用网络服务 soap 的好方法,该 soap 包含一个 xml 信封,带有一些参数和一个二进制 MIME 附件。