繁体   English   中英

允许在SOAP API响应(PHP)中下载PDF文件get(作为附件)

[英]Allow to download PDF file get(as a attachment) in SOAP API response(PHP)

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

如果你在身体中得到二进制响应,只需在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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM