POSTMAN CURL File input corrupted - php

curl --location --request **PUT** 'https://REMOTE_SERVER/1177.png?v=3f75572bc00587e6104ed42d2e16a8f69656fdc63d50be78fb685266a2358340' --header 'Content-Length: 6137' --form 'File=#"/home/user/Documents/1177.png"'
im using postman / php code which creates the above curl , the issue I'm facing is it uploading all type of files but all the files seems to be corrupted and unable to open or load in the browser, following is set up as a remote server
https://modules.prosody.im/mod_http_upload_external.html
only PUT is allowed here to upload files
in case of .txt file it looks good but it addes below code so it looks to me its because of some headers im missing or what not sure can you please help ?
-------------------- SOLUTION ---------------------
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "$remoteSever$fileName?v=$secretAuthToken",
// CURLOPT_UPLOAD => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
// CURLOPT_BINARYTRANSFER => true,
// CURLOPT_HEADER => false,
// CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_PUT => true,
CURLOPT_INFILE => fopen($file->getRealPath(), 'rb'),
CURLOPT_INFILESIZE => $fileSize,
CURLOPT_SSL_VERIFYPEER => false,
// CURLOPT_POSTFIELDS => [
// 'File'=> new \CURLFile($request->file('media_file'))
// // 'File'=> $request->file('media_file')
// ],
// CURLOPT_HTTPHEADER => [
// "Content-Length: $fileSize",
// "Content-Type: $fileMimeType"
// ],
]);
$output = curl_exec($curl);
curl_close($curl);

Related

How to post an audio file from an API call using PHP

I am trying to send an audio voice sms using an API call, but end up getting the error message {"message":"The voice file must be a file.","status":"error"}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://example/api/send',
CURLOPT_HTTPHEADER => ['api-key: keyxxx='],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query([
'recipients' => [
'233550000000'
],
'voice_file' => new CURLFILE('voice_message.mp3'),
]),
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Laravel 8 - "couldn't open file" error when trying to post a s3 stored file to an external API

I am trying to post a file to an external API.
The file is stored in s3.
It works fine when i test it on my local machine and POSTMAN. But it throws an error on the dev server.
Could not figure out what is causing this issue.
The error says
couldn't open file "https://s3.ap-southeast-1.amazonaws.com/files.test.bucket/Applications/1401/1401-Applicant_1_Back_of_Drivers_Licence-07-03-2022_13:59:44-test.jpeg"
Here is the relevant portion of the code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://test.com/api/v1/documents',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('file'=> new \CURLFILE('https://s3.ap-southeast-1.amazonaws.com/files.test.bucket/Applications/1401/1401-Applicant_1_Back_of_Drivers_Licence-07-03-2022_13:59:44-test.jpeg'),'id' => '4688826','documentType' => 'app_drivers_licence_back'),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer 1234567890'
),
));
$documentResponse = curl_exec($curl);

How can I use php-curl to post JSON and a file in multipart/form-data?

I have tried multiple ways of doing this and am having no luck. Any help would be appreciated. I can successfully import the file through postman but the php curl script it generates does not work. These are the instructions from the website:
API imports are sent as form-data type requests, with the request body containing following fields:
importRequest: a text field that contains the request JSON.
files: a file field that contains the import file. For the request header, add a Content-Type header with a value of multipart/form-data.
Here is my current code, generated from postman.
curl_setopt_array($curl, array(
CURLOPT_URL => $apiurl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('importRequest'=>$myJSON,'files'=> new CURLFILE('/C:/Documents/j00119.csv')),
CURLOPT_HTTPHEADER => array(
'Content-Type: multipart/form-data',
'Authorization: Bearer' . $myBearerToken
),
));
$response = curl_exec($curl);
curl_close($curl);

ZOHO projects document API return empty response with status code 201(Created)

I am trying to upload a document using the project API and it's giving an empty response with status 201. Due to an empty response, I am unable to get the id and name of the file uploaded.
curl_setopt_array($curl, array(
CURLOPT_URL => $link,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('uploaddoc'=> new CURLFILE($path)),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$access_token,
"Accept: application/json"
),
));
Link:
https://www.zoho.com/projects/help/rest-api/documents-api.html#alink3

PHP Curl with Knack API - Post an image problem

Does any here have experience with Knack API(https://www.knack.com/) here?
I'm using PHP curl to post an image.
When I use POSTMAN, the image successfully uploads. But when I copy the code from POSTMAN(postman option to copy the source code) and add it to my source code, knack returns empty object error("Please select a file to upload").
Knack API documentation: https://www.knack.com/developer-documentation/#file-image-uploads
The POSTMAN source code below
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.knack.com/v1/applications/app-id/assets/file/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"C:\\Users\\shavk\\Pictures\\292937.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: fe1f4eea-b2b5-2d43-2c3c-c262438866d4",
"X-Knack-Application-Id: id",
"X-Knack-REST-API-Key: key",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
Any help is highly appreciated.
Postman's PHP/curl code generator isn't very good. to upload files in the multipart/form-data-format, use CURLFile.
the code should be
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.knack.com/v1/applications/app-id/assets/file/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'files' => new CURLFile("C:\\Users\\shavk\\Pictures\\292937.jpg")
),
CURLOPT_HTTPHEADER => array(
"X-Knack-Application-Id: id",
"X-Knack-REST-API-Key: key",
),
));
$response = curl_exec($curl);
if you are a customer of Postman (using the pro/whatever version), i suggest you make a complaint to customer support about the terrible code generated by the PHP code generator.

Categories