【发布时间】:2016-02-22 09:25:02
【问题描述】:
PUT - 处理 PDF 时遇到了很多麻烦。我已经设法让它在 Postman 中正常工作,使用下面的代码(大代码块)并通过正文将 PDF 附加为表单数据。我现在正试图在 PHP 中复制它。不过,我在附加 PDF 时遇到了问题。
我尝试了多种技术,试图通过“CURLOPT_INFILE”、“CURLOPT_POSTFIELDS”附加 PDF,但均无济于事。
我通过以下方式创建文件:
$pdf = $_SERVER['DOCUMENT_ROOT'] . '/pdf/temp/temp.pdf';
$file = curl_file_create($pdf, 'application/pdf', 'receipt');`
或
$file = new CURLFile($pdf, 'application/pdf', 'receipt');
我尝试过使用:
$file = fopen($pdf, 'rb');
$file = array('file' => $file);
CURLOPT_POSTFIELDS => $file,
CURLOPT_INFILESIZE => $fileSize,
CURLOPT_INFILE => $file
不过运气不好。
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://staging-tallie.com/v2/enterprise/ENTERPRISEID/MyReceipt/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n-----011000010111000001101001--",
CURLOPT_HTTPHEADER => array(
"accept: application/json; charset=utf-8",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=---011000010111000001101001",
"token: TOKEN",
"upload-filename: receipt.pdf"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
错误读取:
<?xml version="1.0" encoding="utf-8"?>
<ErrorResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ResponseCode>400</ResponseCode>
<Message>Unable to Save the file to the Storage Service.</Message>
</ErrorResponse>
【问题讨论】:
-
PHP进程是否对目标目录有写权限"
-
@DarwinvonCorax 钉在头上!谢谢楼主!