调用SOAP API并使用XML(在下面添加)和一个附件(正文中的二进制PDF不在标头中)给出响应.我想要的是在获得响应时下载该PDF文件.当我在SOAP UI工具中调用API时获取附件.
当我为响应做print_r结果低于响应时
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet
Content-Type: multipart/related; type="text/xml"; start=""; boundary="----
=_Part_16"
Transfer-Encoding: chunked
Date:
------=_Part_
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID:
------=_Part_156_1310882897.1451652608850
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: CR/DEF000000000000000000000785_1
%PDF-1.4
...............
PDF content
...............
出于安全原因,我删除了某些值.
传递标头参数如下
$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?
解决方法:
如果您收到的响应中包含二进制文件,则只需将响应传递到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);
标签:pdf,api,php,soap
来源: https://codeday.me/bug/20191119/2034533.html