php soap 下载文件,允许下载SOAP API响应(PHP)中的PDF文件get(作为附件)

调用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值