How to Convert Curl command to php for -i -t option - php

I would like to convert below curl command into php and i tried to convert from https://incarnate.github.io/curl-to-php/ but its not converting properly. How can i set <myobject> in php curl? that i am not getting. Can anyone help me in this?
curl -i -T <myobject> -X PUT -H "X-Auth-Token: <token>" <storage url>

Looks like you want to use curl to upload a file...
Example
// initialise the curl request
$request = curl_init('http://example.com/');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '#' . realpath('example.txt')
));
// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
// close the session
curl_close($request);

Related

How to correctly code Curl request in PHP with jsonrpc parameters?

I have a curl command that worls perfectly form the SSH shell, but cannot get it working with PHP Version 7.4.3.
I have tried several times using just about every example I can find on the Internet, all of them return a blank html page when run.
Here is the curl command (with user/pass and IP etc changed for obvious reasons);
curl \
--user someuser:password \
--data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' \
-H 'content-type: text/plain;' \
http://195.86.111.23:8223/
Can someone please educate me on how to turn this into something PHP can use?
I am currently testing with this code, but get an error "{"result":null,"error":{"code":-32600,"message":"Params must be an array"},"id":"curltest"} "
$payload=array();
$payload['jsonrpc']='1.0';
$payload['id']='curltest';
$payload['method']='getinfo';
$payload['params']='[]';
$payload=json_encode($payload);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res=curl_exec($ch);
echo $res;
I have also tried using an automated tool here - https://incarnate.github.io/curl-to-php/ which seems to convert my curl command to PHP okay, but the page is empty on loading the provided code.
$payload = ["jsonrpc" => "1.0", id => "curltest", "method" => "getinfo", "params" => [] ];
Simple solution!

cURL PUT Request with Nextcloud / owncloud API

I tried to update an existing Nextcloud user through their API. When I do it directly via shell it works
curl -u user:pass -X PUT "https://example.org/ocs/v1.php/cloud/users/admin" -H "OCS-APIRequest: true" -d key="quota" -d value="5GB"
But when I try to do it via PHP with the following code it always returns "failure 997"
$url = 'https://' . $ownAdminname . ':' . $ownAdminpassword . '#example.org/ocs/v1.php/cloud/users/admin';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$fields = array("quota" => "5GB");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'OCS-APIRequest: true'
));
$response = curl_exec($ch);
curl_close($ch);
echo "Response: ".$response;
The difference between the cURL command and the PHP code you pasted lies in a poorly designed user provisioning API.
Using these cURL arguments:
-d key="quota" -d value="5GB"
... is not equivalent to the fields you're posting:
$fields = array("quota" => "5GB");
... but rather:
$fields = array(
'key' => 'quota',
'value' => '5GB',
);
The explanation for the 997 code you're getting can be found in https://github.com/owncloud/core/blob/v10.0.3/apps/provisioning_api/lib/Users.php#L269-L272: since there's no "key" key in the data submitted ($parameters['_put']['key'] will evaluate as null) and hence the error.

Curl works on command line but not in PHP.

I'm trying to upload a .csv file to an API. The API documentation says to post it like this:
curl --user username:password
--request POST
--data-binary #yourfile.csv
https://api.example.com/api
This works perfectly.
in PHP I have it like this:
$data = array('myfile' => 'myfile.csv');
$curl = curl_init('https://api.example.com/api');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_USERPWD, "username" . ":" . "password");
$result = curl_exec($curl);
echo $result;
The results do equal what the API documentation says is the correct response (and is the same response as when using the command line) however it does not actually upload the contents of the file.
is my PHP structure correct? Thanks.

How to pass array of arguments to cURL in command line?

I got desired response when i send cURL request from my PHP script.
My request is like this.
$data = array ("[product[name]]" => "nw",
"[product[handle]]" => 150,
"[product[interval_unit]]" => "day",
"[product[interval]]" => 1,
"[product[price_in_cents]]" => 0,
"[product[initial_charge_in_cents]]" => 14200,
"[product[return_url]]" =>"http://mytrial.com/office/selfie/themes/adcampaign/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d",
"[product[return_params]]" => "id={subscription_id}&customer_id={customer_id})");
$url="http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, 'sdfkjas2kjsd:x');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
It's working properly. I want to do the same request in command line.First i json encoded the array and i tried with this commands
curl -u sdfkjas2kjsd:x -H Accept:application/json -H Content-Type:application/json -x POST --data product[name]=nw&product[handle]=142&product[interval_unit]=day&product[interval]=1&product[price_in_cents]=0&product[initial_charge_in_cents]=14400&product[return_url]=http:\/\/54.145.218.63\/dev_lpad\/launchpad\/advertisers\/adcampaign\/56cee935-185c-4349-a8a1-2b6b0ae84a4d&product[return_params]={id={subscription_id}&customer_id={customer_id}} http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json
Then i got the error.
Error: Unable to parse request body
Is there any way to solve this?
UPDATE : The URL provided here is a dummy value,Actually i am trying to connect with Chargify API (Recurring billing solution ).
It seems that your server does accept json payload post data. You probably forgot to json_decode your data, here is the fix:
curl -u sdfkjas2kjsd:x -H Accept:application/json -H Content-Type:application/json --data '{"product":{"name":"nw","handle":150,"interval_unit":"day","interval":1,"price_in_cents":0,"initial_charge_in_cents":14200,"return_url":"http:\/\/mytrial.com\/office\/selfie\/themes\/adcampaign\/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d","return_params":"id={subscription_id}&customer_id={customer_id})"}}' http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json
If i send it to my php script <?php var_dump(json_decode(file_get_contents('php://input'))); I see correct answer:
object(stdClass)#1 (1) {
["product"]=>
object(stdClass)#2 (8) {
["name"]=> string(2) "nw"
["handle"]=> int(150)
...
Finally I could solve this issue by splitting the array parameters. My cURL cummand is this.
curl -u sdfkjas2kjsd:x -d 'product[name]":nw' -d '[product[handle]]=161' -d '[product[interval_unit]]=day' -d '[product[interval]]=1' -d '[product[price_in_cents]]=0' -d '[product[initial_charge_in_cents]]=14200' -d '[product[return_url]]=http:\/\/mytrial.com\/office\/selfie\/themes\/adcampaign\/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d' -d 'product[return_params]=id={subscription_id}&{customer_id={customerC_id}})' http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json
I think you should put your data inside single quotes
curl ... --data 'some data here' ...
EDIT:
ON WINDOWS the proper way to pass array argument via cURL is shown below:
curl -X POST http://localhost:8080/uploadMultipleFiles -H "content-type: multipart/form-data" -F "files=#C:\Users\...\Desktop\filename1.txt,C:\Users\...\Desktop\filename2.txt,C:\Users\...\Desktop\filename3.txt,C:\Users\...\Desktop\filename4.txt
See the use of comma separated filenames where the server expect files to be an Array of Files.

cURL: from PHP to BASH

I've never done any curl before so am in need of some help.
php:
<?php
$ch = curl_init();
$data = array(
'uptype'=>'file',
'file'=>'#'.$argv[1],
);
curl_setopt($ch, CURLOPT_URL, 'http://my_site_ex/up.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
curl_close($ch);
?>
how to make the same script in BASH?
I believe it's:
curl -F "uptype=file" -F "file=#$1" 'http://my_site_ex/up.php'
The -F uses multipart/form-data, which the PHP interface libcurl uses if you pass an array for CURLOPT_POSTFIELDS. Each -F is a separate field. libcurl reads the file you specify with #.
I belive its like so
data='-F "uptype=file" F "file=#$1"'
server="http://my_site_ex:8080/up.php"
opts="-v"
curl $server $opts $data
Im not 100% unfortunately but its something along these lines.

Categories