首页 > 解决方案 > 带有标准 xml 信封和 pdf 附件的肥皂服务的 CURL 请求

问题描述

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

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

在我看来,呼叫的内容是相同的,但我继续收到一个肥皂错误。

这是呼叫请求设置:

$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 = 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);

任何建议将不胜感激。

标签: phpweb-servicescurlsoapattachment

解决方案


可以认为问题已解决。

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

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


推荐阅读