Trouble converting POST curl from command line to php - php

I am having trouble converting my curl command into php.
This part works great.
CURL command that adds an entry into my Parse.com database:
curl -X POST \
-H "X-Parse-Application-Id: my_id" \
-H "X-Parse-REST-API-Key: api_id" \
-H "Content-Type: application/json" \
-d "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}" \
https://api.parse.com/1/classes/MyClass
SOLVED ANSWER:
I have created this php script to replicate the command:
<?php
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array('X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");
curl_exec($ch);
curl_close($ch);
?>

You've missed some crucial configurations.
These are the set the CURL to send request using POST, and the second is data to send. (RAW DATA is being sent as string into POSTFIELDS, those if you send array - it will automatically append header "multipart/form-data"
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");
curl_exec($ch);
curl_close($ch);
HTH:)

Since you are doing a POST request you need to tell Curl to do that as well:
$postData = '{"SiteID":"foundID","dataUsedString":"foundUsage","usageDate":"foundDate", "monthString":"foundMonth", "dayString":"foundDay","yearString":"foundYear"}';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
You may also need to supply a Content-Length header:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Parse-Application-Id: my_id',
'X-Parse-REST-API-Key: api_id',
'Content-Type: application/json',
'Content-Length: '.strlen($postData))
);

Related

File upload CURL command to PHP Curl

I have CURL command I need to use him in php page how I can change it. I have no idea about it.
The command :
curl --request POST \
--url 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg' \
--header 'authorization: Bearer BEARER_TOKEN_HERE' \
--header 'content-type: image/jpeg' \
--data "#/path/to/local-file.jpg"
For your curl command, Please try this PHP :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'file' => '#' .realpath('/path/to/local-file.jpg')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Authorization: Bearer BEARER_TOKEN_HERE';
$headers[] = 'Content-Type: image/jpeg';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
?>
Please also study the reference provided by Professor Abronsius if you have time. It's worth the effort.

Why the PHP curl is not working? When it works from command line curl?

When i do it from command line as below it works:
$ curl -X "POST" "https://ABCD/login/oauth2/access_token" -H "Authorization: Basic XXXX=" -H "Content-Type:application/x-www-form-urlencoded" --data-urlencode "realm=XXX" --data-urlencode "XXX=XXX"
But when i do it from PHP its not working:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ABCD/login/oauth2/access_token');
curl_setopt($ch, CURLOPT_POST, 1);
$ss = array(
'realm' => 'XXX',
'XXX'=>'XXX'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Authorization: Basic XXXX=',
'Content-Type: application/x-www-form-urlencoded',
));
$result=curl_exec ($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
Any idea what is that i am doing wrong? I get HTTP status that "Required parameters or body is missing or incorrect."
Your command line says --data-urlencode — URL encoding
Your PHP says 'Content-Type: application/x-www-form-urlencoded', — so you say you are URL encoding the data
It also says curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss)); — so you are JSON encoding it and not URL encoding it.
Send the URL encoded data you claim to be sending.

CURL works with Terminal but showing an error within PHP code?

When I use the terminal for posting an image using CURL it works fine but when I transform it within PHP code it throws an error 400 Bad Request.
CURL code using Terminal
curl -H "Authorization: Bearer ${access_token}" \
-H "Content-Type: image/jpeg" \
-i \
-X POST \
--data-binary #my_image.jpg \
https://example.com/objects/v1/files
CURL code within PHP file
$headers = array(
'Content-Type: image/jpeg',
'Authorization: Bearer ' . $accessToken,
);
$binary = 'my_image.jpg';
$ch = curl_init('https://example.com/objects/v1/files');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '#'.$binary);

Create a cURL request using PATH API

Guys. I am new to CURL so i have no experience on implementing CURL request. In this case, i want to post some data using CURL. Here is the CURL :
curl -H 'Authorization: Bearer <ACCESS_TOKEN>' \
-H 'Content-Type: application/json' \
-d '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}' \
https://partner.path.com/1/moment/photo
is there any of you guys know about implementing CURL request using the above data? Thank you very much.
finally, i found the answer. Here is how i create a request to the Path API
$url = 'https://partner.path.com/1/moment/photo';
$authorization = "Authorization: Bearer 8edf232243d58e4940d931490e882123432434f";
$headers = array($authorization,'Content-Type: application/json');
$json_data = '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );

translating a command line curl into php

i am not very curl savvy was wondering if anyone could help me turn the following into php:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}' https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret
if you know of a good curl tutorial would also be great help.
something like this
$ch = curl_init();
$json = '{"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}';
$url = 'https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret';
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

Categories