通过curl请求我将pdf数据作为jsonEncoded byte []获取。我需要创建并将此数据保存为pdf。
你可以帮助我在php中如何做到这一点。
我的卷曲代码是 -
$ch = curl_init($url);
curl_setopt_array($ch,
array( CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_BINARYTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ),
CURLOPT_POSTFIELDS => json_encode($postData) )
);
$response = curl_exec($ch);
$responseData = json_decode($response, TRUE);
当我打印responseData
时,它会将此作为结果 -
JVBERi0xLjQKJeLjz9MKMSAwIG9iaiAKPDwKL01hdHJpeCBbMSAwIDAgMSAwIDBdCi9TdWJ0eXBlIC9Gb3JtCi9GaWx0ZXIgL0ZsYXRlRGVjb2RlCi9MZW5ndGggMTUwMzc3Ci9SZXNvdXJjZXMgCjw8Ci9FeHRHU3RhdGUgCjw8Ci9HUzAgMiAwIFIKL0dTMSAzIDAgUgo+PgovQ29sb3JTcGFjZSAKPDwKL0NTMCA0IDAgUgo+PgovUHJvcGVydGllcyAKPDwKL01DMyAKPDwKL01ldGFkYXRhIDUgMCBSCj4+Ci9NQzIgCjw8Ci9NZXRhZGF0YSA2IDAgUgo+PgovTUMxIAo8PAovTWV0YWRhdGEgNyAwIFIKPj4KL01DMCAKPDwKL01ldGFkYXRhIDggMCBSCj4+Cj4+Ci9YT2JqZWN0IAo8PAovRm0xIDkgMCBSCi9GbTAgMTAgMCBSCi9GbTIgMTEgMCBSCj4+Ci9Gb250IAo8PAovVDFfMyAxMiAwIFIKL1QxXzIgMTMgMCBSCi9UMV8xIDE0IDAgUgovVDFfMCAxNSAwIFIKPj4KL1Byb2NTZXQgWy9QREYgL1RleHRdCj4+Ci9Gb3JtVHlwZSAxCi9UeXBlIC9YT2JqZWN0Ci9CQm94IFswIDAgNjEyIDc5Ml0KPj4Kc3RyZWFtCnic3L1bjy09ciX2/v2K/WgZ2KXknQQEAR5JbdiGgBmogXkYG4askWZst2TrMrDf5q+bwSCZychgXqoqso6MBvo7depUcW/uJNeK21r/+Nsf//d/tbz+0z//9se/+/vl9ef/12//7rc//qv/+6//4fUnf/LHf/ln/8Ofv5bXn/7pv/nzP3v99m9+/9vygv+p1/8JP6XKT/1e/a/wN7//u99S+WZ6Was+lA2vYPTHovMffv/3v/2H/+Z/Xkz4o7eO8Aff/gB/o9sf4L8u/9fBH2z+g4E/mPYH/Udv5eEPKv8hwB+W/AdT/2b4aZ1/nVq2P63zkirBH1z9aW3pDxn6F7q9TFV==
答案 0 :(得分:3)
生成的数据字符串是base64 - 编码数据。因此,要进一步处理它,您需要调用base64_decode( $responseData );
1 。
如果数据未损坏,并将其保存到文件中,则应获得有效的PDF文件。
例如:
$responseData = json_decode($response, TRUE);
// decode the base64 data
$pdfData = base64_decode( $responseData );
// save the decoded data to a PDF file
file_put_contents( '/path/to/file.pdf', $pdfData );
PDF文件现在应保存在/path/to/file.pdf
。
答案 1 :(得分:0)
尝试像这样更改HTTP标头:
$ch = curl_init($url);
curl_setopt_array($ch,
array( CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_BINARYTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Content-type:application/pdf'),
CURLOPT_POSTFIELDS => json_encode($postData) )
);
$response = curl_exec($ch);
$responseData = json_decode($response, TRUE);