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
Related
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 am trying to post a data to REST API using PHP/CURL and seems it's not working, as i get 301 Moved Permanently error,But it works fine with Postman. Here is the snippet of PHP CURL which i generated form Postman
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'api 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 => 'POST',
CURLOPT_POSTFIELDS => array('text' => 'test'),
CURLOPT_HTTPHEADER => array(
'api_key: key',
'Content-Type:application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it always return 301 Moved permanently, Note if i change the API key i got Unauthorised error, which means it hits the server, but i am not sure what i am missing, I have tried with multiple headers combinations.
Any help on this regard would be highly appreciated.
Quoting the PHP manual :
CURLOPT_POSTFIELDS: This parameter can either be passed as a urlencoded
string like 'para1=val1¶2=val2&...' or as an array with the field
name as key and field data as value. If value is an array, the
Content-Type header will be set to multipart/form-data.
Since you use array you'll need to use multipart.
See this post : curl POST format for CURLOPT_POSTFIELDS
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...
I am trying to send a Curl request to Wit.ai speech.
I am recording a .wav file on the frontend with recorder.js.
I specified mono channel, since wit.ai only supports mono.
I know that php7 doesn't support # for CURLOPT_POSTFIELDS, so I downgraded my php to 5.6 just to keep it simple.
I used Postman to generate the Curl request.
I also saw this Post which his similar to my issue but I am using a file: How to stream speech to wit.ai speech end point
Note: What I think, I am still lacking wav file parameters, but I am not sure how to get them. I tried properties but there wasn't much info.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.wit.ai/speech?v=20170307",
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_SAFE_UPLOAD => false,
CURLOPT_POSTFIELDS =>"#uploads/audio1585176568858.wav",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $token",
"Content-Type: audio/wav"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response
The response I get back is this : { "_text": "", "entities": {} }.
I also used a python script with https://pypi.org/project/SpeechRecognition/ lib with same audio .wav file and it worked.
I am referring to o365 API documentation for adding push notification of calendar event. I am getting below error-
Post>>
array:4 [▼
"#odata.type" => "#Microsoft.OutlookServices.PushSubscription"
"Resource" => "https://outlook.office.com/api/v2.0/me/events"
"NotificationURL" => "https://mywebsite.com/notifications"
"ChangeType" => "Created,Updated"
]
Response>>
"{"error":{"code":"ErrorInvalidParameter","message":"Notification URL 'https://mywebsite.com/notifications?validationtoken=ODFkNDllYWEtMmExYi00NDVjLWJmNzUtOTBhZjg3MDAyNjhh' verification failed because the response body '\tsuccess\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000' is unexpected."}} ◀"
My code is as-
$post_data = [ "#odata.type"=> "#Microsoft.OutlookServices.PushSubscription",
"Resource"=> "https://outlook.office.com/api/v2.0/me/events",
"NotificationURL"=> 'https://mywebsite.com/notification',
"ChangeType"=> "Created,Updated",
// "expirationDateTime"=> "2018-05-09T18:23:45.9356913Z",
// "clientState"=> "testClientState"
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://outlook.office.com/api/v2.0/me/subscriptions",
//CURLOPT_URL => "https://graph.microsoft.com/beta/subscriptions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($post_data),
CURLOPT_HTTPHEADER => array(
// Set Here Your Requesred Headers
'Authorization: Bearer '.TOKEN,
'Content-Type: application/json',
'X-CSRF-TOKEN: TOKEN-XXXXX'
),
));
How can I resolve the error?
Thanks in advance.
Your notification endpoint must respond to a validation POST in a very specific way. Your response doesn't match that way, so the validation is failing.
From https://developer.microsoft.com/en-us/graph/docs/concepts/webhooks:
Microsoft Graph validates the notification URL in a subscription request before creating the subscription. The validation process occurs as follows:
Microsoft Graph sends a POST request to the notification URL:
POST https://{notificationUrl}?validationToken={TokenDefinedByMicrosoftGraph}
ClientState: {Data sent in ClientState value in subscription request (if any)}
The client must provide a response with the following characteristics within 10 seconds:
A 200 (OK) status code.
The content type must be text/plain.
The body must include the validation token provided by Microsoft Graph.
The client should discard the validation token after providing it in the response.