2

I'm send file type PDF and Json to API server, but for the file PDF after upload like corrupt the file size is 0 byte and it can't be open.

I try send post with PHP CURL where the code generate from Postman, but if i send with postman POST the file is fine (not damage, can open)

<?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;
}

what i'm wrong to locate filename the PDF or missing something ?

  • where did you run the codes? on server? or your local pc? – ariefbayu Jan 17 '19 at 05:09
  • also, can you try the solution here: https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php – ariefbayu Jan 17 '19 at 05:11
  • I run code from local to save file to server, i do same with postman(from local where is successfull upload the pdf). That solution have i tried but not working for me, still uploaded file 0 byte –  Jan 17 '19 at 06:13

0 Answers0