Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ).

like image 259
ARUNBALAN NV Avatar asked Mar 10 '16 10:03

ARUNBALAN NV


2 Answers

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)
  ...
like image 109
shukshin.ivan Avatar answered Nov 12 '22 08:11

shukshin.ivan


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
like image 45
ARUNBALAN NV Avatar answered Nov 12 '22 08:11

ARUNBALAN NV