我正在将文件类型PDF和Json发送到API服务器,但是对于文件PDF如上传后损坏,文件大小为0字节,无法打开。
我尝试使用PHP CURL发送从Postman生成代码的帖子,但是如果我使用Postman POST发送文件,则文件很好(没有损坏,可以打开)
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://Url to API",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7asdzxcsqljas1\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\Users\\file.pdf\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7asdzxcsqljas1\r\nContent-Disposition: form-data; name=\"formJson\"\r\n\r\n{ \"Json\":\n{\n\"key1\" : \"key1\",\n\"key2\" : \"key2\",\n}\n]\n}\n}\r\n------WebKitFormBoundary7asdzxcsqljas1--",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ...",
"Content-Type: multipart/form-data",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7asdzxcsqljas1"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
找不到文件名PDF或缺少某些内容是什么错误?