调用SOAP API,它正在使用XML(下面添加)和一个附件(主体中的二进制文件而不是标题中的PDF)给出响应。我想要的是在获得响应时下载该PDF文件。在SOAP UI工具中调用API时获取附件。
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns2:response xmlns:ns2="http://url.com/"/>
</env:Body>
</env:Envelope>
当我执行print_r以获得低于响应的响应时
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet
Content-Type: multipart/related; type="text/xml"; start="<@.org>"; boundary="----
=_Part_16"
Transfer-Encoding: chunked
Date:
------=_Part_
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <r.............>
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Header></env:Header>
<env:Body>
<ns2:Response xmlns:ns2="http://url.com/"/></env:Body>
</env:Envelope>
------=_Part_156_1310882897.1451652608850
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: CR/DEF000000000000000000000785_1
%PDF-1.4
...............
PDF content
...............
出于安全原因,我删除了一些值。
传递Header参数如下
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: application/pdf",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: $API_URL",
"Content-length: ".strlen($request_xml),
);
并传递curl选项,如下所示
$ch = curl_init($API_URL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$request_xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
是否有任何问题或我如何以PDF格式解析响应正文中的数据?
答案 0 :(得分:3)
如果你在身体中得到二进制响应,只需在PDF文件中传递你的回复。
$ch = curl_init($API_URL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$request_xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Encoding: none');
header('Content-Type: application/.pdf');
header('Content-Disposition: attachment; filename='.$filename.'.pdf');
$destination = dirname(__FILE__) . '/'.$filename.'.pdf';
$file = fopen($destination, "w+");
fputs($file, $response);
fclose($file);
readfile($destination);