I'm having trouble sending a file to api via curl.
I have a code that was generated in postman but unfortunately it doesn't work. I got the libraries for postman from the producer.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.accept.autenti.net/api/v0.1/documents/*********************/files",
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('files'=> new CURLFILE('/path/to/file'),'files'=> new CURLFILE('/path/to/file')),
CURLOPT_HTTPHEADER => array(
"Authorization: *********************",
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
server response:
{"error":{"id":"8fca0a50-8e8f-48e7-9855-30d27fdd45fd","key":"NO_FILES_GIVEN"}}
I modified it according to the documentation. I have added to CURLOPT_HTTPHEADER
"Content-Type: multipart / form-data; boundary = Content-Disposition: form-data; name = 2665; Content-Type: application / pdf",
"Content-Length: 192897"
And at the moment it looks like this:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.accept.autenti.net/api/v0.1/documents/*********************/files",
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('files'=> new CURLFILE('2665.pdf')),
CURLOPT_HTTPHEADER => array(
"Authorization: *********************",
"Content-Type: multipart/form-data; boundary=Content-Disposition: form-data; name=2665; Content-Type: application/pdf",
"Content-Length: 192897"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
server response:
{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}
Here is the link to the documentation : https://autenti.com/api/ Upload.
that's a bug in postman, the generated code makes no sense, you should send a bugreport to postman, if you can be arsed.
CURLOPT_POSTFIELDS => array(
'files' => new CURLFILE('/path/to/file'),
'files' => new CURLFILE('/path/to/file')
)
is barely legal, and nonsensical php code, perhaps it was supposed to be
CURLOPT_POSTFIELDS => array(
'files' => array(
0 => new CURLFILE('/path/to/file'),
1 => new CURLFILE('/path/to/file')
)
)
also if it's indeed a multipart/form-data-post, this header is just wrong, a bug in the generator most likely: "Content-Type: application/x-www-form-urlencoded"
and do not set the "Content-Type: multipart / form-data; boundary-header manually, instead let curl generate it for you automatically, also do not set the "Content-Length: 192897" header manually, it's VERY likely to be incorrect, as the length depends on the file size AND the length of the boundary AND even the filename of the file you're uploading, and curl will generate the header for you automatically if you don't (and curl won't make any mistakes in the length headers, unlike you)
the
{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}
error could mean that you actually exceeded the limit, but it's more likely that your hardcoded Content-Length: 192897-header has a bogus value, and the bogus content-length header value made the server confused and generate the "FILE_SIZE_LIMIT_EXCEEDED"-error.
but skimming through the documentation, looks to me like it should be:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL=> 'https://api.accept.autenti.net/api/v0.1/documents/*********************/files',
CURLOPT_HTTPAUTH => CURLAUTH_BEARER,
CURLOPT_XOAUTH2_BEARER=>"token",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS=>array(
new CURLFile($filename, "application/pdf")
)
));
curl_exec($ch);
... and that their example code is terrible. here is the request generated by the above code:
POST /api/v0.1/documents/*********************/files HTTP/1.1
Host: api.accept.autenti.net
Authorization: Bearer token
Accept: */*
Content-Length: 193
Content-Type: multipart/form-data; boundary=------------------------d058e8488f15fa10
--------------------------d058e8488f15fa10
Content-Disposition: form-data; name="0"; filename="test.file"
Content-Type: application/pdf
lol
--------------------------d058e8488f15fa10--
which looks very close to what they want in the sample code...
Related
Here is the trouble. I have I cant seen to get it right. Any time I try to send a post to the api call.
https://www.zoho.com/mail/help/api/put-verify-domain.html
if shot me a error of JSON_PARSE_ERROR.
here is the body of the curl
[{"mode":"verifyDomainByCName"}]
curl header "Authorization: Zoho-oauthtoken $token", 'Content-Type: text/plain'
If I try this
{"mode":"verifyDomainByCName"}
I get a HTTP Status 415 – Unsupported Media Type
the base setting for curl
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_HTTPHEADER => $this->header,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $this->data,
Thank you for any help
Karl K
Tried to run a script with a json body and keep getting error
I need to PUT some json data to an API endpoint, which works as expected via command line curl, but not via php curl and I don't have any idea, why it doesn't.
my command is
curl -v --insecure --request PUT --url <https://blabla/blablabla> --user 'username:password' --header 'Content-Type: application/json' --data '<valid json data>'
but it doesn't work this way within php:
// get cURL resource
$curl = curl_init();
// set cURL options
$curloptions = array(
CURLOPT_PUT => true, // set method to PUT
CURLOPT_RETURNTRANSFER => true, // return the transfer as a string
CURLOPT_VERBOSE => true, // output verbose information
CURLOPT_SSL_VERIFYHOST => false, // ignore self signed certificates
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERNAME => $config['uag']['user'], // set username
CURLOPT_PASSWORD => $config['uag']['pass'], // set password
CURLOPT_HTTPHEADER => array( // set headers
"Content-Type: application/json",
),
CURLOPT_POSTFIELDS => $jsondata // set data to post / put
);
curl_setopt_array($curl, $curloptions);
foreach($serverurilist as $uri) {
// set url
curl_setopt($curl, CURLOPT_URL, $uri);
// send the request and save response to $response
$response = curl_exec($curl);
// stop if fails
if(!$response) {
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
var_dump($response);
}
// close curl resource to free up system resources
curl_close($curl);
What doesn't work? The payload / data doesn't get submitted. If I tcpdump the command line und php version without encryption, I can see, that the command line submits the data right after the Expect: 100-continue request and the HTTP/1.1 100 Continue response from the server. The php version doesn't do anything after the HTTP/1.1 100 Continue response and quits after reaching the timeout.
From documentation:
CURLOPT_PUT - true to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.
and you are not using any file to provide content.
You should use CURLOPT_CUSTOMREQUEST => 'PUT'.
This is your same cUrl request exported from Postman:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://blabla/blablabla',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>'<valid json data>',
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ='
],
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
You should use this CURLOPT_CUSTOMREQUEST=>'PUT'
I got the issue in integration of route mobile sms gateway. In URL i send the message but URL does not accept space so encode the message by using encode function, then URL accept the message but after using encode, SMS also receive as encoded.
please check the code.
$text = "" . $sms_message . " " . $user_data['otp'] . "";
$message = $this->myUrlEncode($text);
$URL = "http://rslr.connectbind.com:8080/bulksms/bulksms?username=$username&password=$password&type=0&dlr=1&destination=$number&source=$source&message=$message";
curl_setopt_array($curl, array(
CURLOPT_PORT => "8080",
CURLOPT_URL => $URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 30,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Connection: keep-alive",
"Host: rslr.connectbind.com:8080",
),
));
$response = curl_exec($curl);
So, what should I do to solve this problem?
Not sure what your encode function does, but if you just wrap ‘urlencode’ you can use ‘urldecode’.
$response = urldecode(curl_exec($curl));
I have the following php code from my API console on RapidApi working correctly:
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://webit-computer-vision.p.rapidapi.com/describe",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"image\"\r\n\r\n\r\n-----011000010111000001101001--\r\n\r\n",
CURLOPT_HTTPHEADER => [
"content-type: multipart/form-data; boundary=---011000010111000001101001",
"x-rapidapi-host: webit-computer-vision.p.rapidapi.com",
"x-rapidapi-key: XXXXXXXXXX"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
The only problem is that it also includes a file which is sent to the endpoint. How can I upload file to the given endpoint? The content-type appears to already be set correctly. I believe I just need to send the content of file in addition.
for anyone wondering, I was able to get it working by modifying the header with this code:
CURLOPT_HTTPHEADER => [
"content-type: multipart/form-data",
"x-rapidapi-host: webit-computer-vision.p.rapidapi.com",
"x-rapidapi-key: VMssAjUwitmshMGT0yv1SbvOhkLVp1f3BaqjsnpKZ8t9yLcbxs"
]
and then had to change the following to get the image file to POST properly:
$fields = [
'image' => new \CurlFile("images/2021_04_01_Ied8W_Ex0P-meVoActjCQ.jpg", 'image/jpeg', 'filename.jpg')
];
...
CURLOPT_POSTFIELDS => $fields,
...
started working pretty quickly after that =)
everybody,
I am using the following code for synchronizing data with an API ($method= POST) and then I call using the GET method ($method= GET) to print the result of the synchronization:
function sendCurl ($fileName, $method)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => array('file' => new CURLFILE($fileName)),
CURLOPT_HTTPHEADER => array(
"Authorization: xxxxxxxxxxxxxxx",
"Accept: application/json."
"Content-Type: multipart/form-data"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
The problem is that the GET method response arrives before the API has time to process it. Therefore, when I print the response I print WAITING FOR RESPONSE and not whether it went ok or failed.
What can I do to print the ok or fail and not the WAITING FOR RESPONSE?
Thank you very much!