How to properly uploads files to certain api uisng curl in php - php

Am trying to add/upload files to monday.com api via curl in php but the code throws error {"error_message":"Internal server error","status_code":500}
Monday Docs
here is the API Curl request
curl --location --request POST 'https://api.monday.com/v2/file' \
--header 'Authorization: YourSuperSecretAPIKey' \
--form 'query="mutation add_file($file: File!, $item_id: Int!, $column_id: String!) {add_file_to_column (item_id: $item_id, column_id: $column_id, file: $file) {id}}"' \
--form 'variables="{\"item_id\":1104589438, \"column_id\": \"files\"}"' \
--form 'map="{\"image\":\"variables.file\"}"' \
--form 'image=#"/Users/alexsavchuk/Downloads/300px-All_Right_Then,_Keep_Your_Secrets.jpg"'
Arguments Description of the API Variable
item_id !Int The item ID where the file will be added.
column_id !String The column where the file will be added.
file !File The file to upload.
Here is my coding efforts
$token = 'Token goes here';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.monday.com/v2/file',
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('query' => 'mutation ($file: File!) { add_file_to_column(file: $file, item_id: 279111, column_id: "files") { id } }','variables[file]'=> new CURLFILE('https://cdn.pixabay.com/photo/2015/05/26/12/30/baby-784608_960_720.jpg')),
CURLOPT_HTTPHEADER => array('Authorization: ' . $token)));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Related

PHP curl PUT does not continue respectively send payload/data

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'

PHP Get Access an Token from Gotowebinar API

I try using PHP to get an access token in GoToWebinar API, from this documentation https://developer.goto.com/guides/HowTos/03_HOW_accessToken/
here my php script
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.getgo.com/oauth/v2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'Authorization: Basic MWM4ODE2MmItMGZkZS00YTM4LWI0NGMtMWMzOGFhMDY5YmI4OktQSDg2d1hnVUM5a0FqbithMVBsOGc9PQ==',
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS =>"redirect_uri=http://indotourismevents.com/webinar&grant_type=authorization_code&code=eyJraWQiOiJvYXV0aHYyLmxtaS5jb20uMDIxOSIsImFsZyI6IlJTNTEyIn0.eyJscyI6IjczYjkyNzhmLTBmNjMtNDE3NC1iYTljLWE3ZTIyYzIwMDhmNyIsInVyaSI6Imh0dHA6Ly9pbmRvdG91cmlzbWV2ZW50cy5jb20vd2ViaW5hci8iLCJzYyI6ImNvbGxhYjogaWRlbnRpdHk6c2NpbS5tZSBpZGVudGl0eToiLCJhdWQiOiIxYzg4MTYyYi0wZmRlLTRhMzgtYjQ0Yy0xYzM4YWEwNjliYjgiLCJzdWIiOiIzODc4MTIxOTUzODYzNTgxMTkwIiwianRpIjoiOTgyYTYyMWQtYTFhYi00NTE3LWFlYTgtNDFlYmY3OWE0NzFlIiwiZXhwIjoxNTk0NzQxOTUxLCJpYXQiOjE1OTQ3NDEzNTEsInR5cCI6ImMifQ.Cy6TR18iKmCtotZnnPvMUS9QMh4EFBP6FMcqBOWN9LtricreNg1qnA45UJB0cQALB1d5h9zgc-nDkU4GtTNr8ZnDSI26On3YqFJBICptmfrHhI7ZZIwH-p8YilKirHua1shEPVpradyAj_epjfmejd35QbCXD8aTz3uq-ocRGZFvz0WZfAA7wWryMBnkvTm5BM4fTx99Q4AGfT27QFomaR4hGsLy6uUM4N3rcqP7VNM21XeuIZ-5U1zd2Ew0-gnWoNo8lOF1hJeihZ_xyMlY9AgN7HAwW55JmUkFSsNVHdLznYebzXuDJZXGYnXxQwIyVym3tmx88JRUcI0bMzJ5-w",
));
$response = curl_exec($curl);
$err = curl_error($curl);
from the documentation, curl must give some header and form body
curl -X POST "https://api.getgo.com/oauth/v2/token" \
-H "Authorization: Basic YTIwfAKeNGYtODY4YS00MzM5LTkzNGYtNGRhMmQ3ODhkMGFhOjNuYU8xMElBMmFnY3ZHKzlJOVRHRVE9PQ==" \
-H "Accept:application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "redirect_uri=https://example.com&grant_type=authorization_code&code=iS0vynEEvRFA9i6kZ8gvNDnnOGE..."
and i give all required header&form body, but still return an error like this
{"error":"invalid_request","error_description":"Required parameter(s) missing or wrong."}
how to PHP get access token from GoToWebinar API?
I experienced this problem when I logged in with the same user account to G2Webinar as my developer user (the oauth client's user was the same as the user webinar user)
Try to register another account.

PHP CURL does not return the full token

I am using PHP curl to do basic Auth that return a jwt token.
When I Request the token in shell using curl i get a token of length 381 .However when I do the request in my php code i only get a token of length 326 which is not complete which lead to other problems in my other php requests .
how can i make my php code give me the full length of my token that is 381 in my response and not just 326?
Update my cURL shell code is :
curl --location --request POST 'https://xxxx/xxxx/auth/login' \
--header 'Authorization: Basic XXXXX'
my curl php code :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxxxx/xxxxx/auth/login",
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_HTTPHEADER => array( "Authorization: Basic xxxxxxx" ),
) );
$response = curl_exec($curl);
curl_close($curl); echo $response;
Thanks

How to write curl -X PUT -T script in php

I am working on a Hootsuite script that needs to upload a file using cURL PUT request. On Hootsuite site, I found below example code but I am being able to convert below code in PHP script :
curl -X PUT -H 'Content-Type: video/mp4' \
-H "Content-Length: 8036821" \
-T "/Users/kenh/Downloads/amazing_race.mp4" \
"https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D"
I tried below code but it is still not working :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => array('file' => '#' . realpath('/Users/kenh/Downloads/amazing_race.mp4')),
CURLOPT_HTTPHEADER => array(
'Content-Type: video/mp4',
'Content-Length: 8036821'
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
So can anybody help me out by telling how to convert above code in PHP ?
I'll be thankful.
Try appending http_build_query on the CURLOPT_POSTFIELDS option, like below :
CURLOPT_POSTFIELDS => http_build_query( array( 'file' => '#' . realpath( '/Users/kenh/Downloads/amazing_race.mp4' ) ) ),
From what I know, the http_build_query is required on the post fields when using PUT method.

Error executing curl from php

The following code runs perfectly using POSTMAN. While I try to use the code generated by postman for PHP, it gives me errors.
Original Code:
curl -X POST \
'http://111.93.2.38:8080/gsp/gsp/authenticate?grant_type=token' \
-H 'cache-control: no-cache' \
-H 'gspappid: 64AB7894D61644F3B5CA18B4738C700D' \
-H 'gspappsecret: 70988876GEEEEG4504GB400G92B801E23686' \
PHP Code:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8080",
CURLOPT_URL => "http://111.93.2.38:8080/gsp/gsp/authenticate?grant_type=token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"gspappid: 64AB7894D61644F3B5CA18B4738C700D",
"gspappsecret: 70988876GEEEEG4504GB400G92B801E23686",
"postman-token: ff8278bb-b888-9fd3-6a55-780dafd58aa5"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Please help me.
What kind of error are you facing , can you please share so we can help you?
If there is timeout error , you can try changing value of CURLOPT_TIMEOUT => 30

Categories