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

ARUNBALAN NV picture ARUNBALAN NV · Mar 10, 2016 · Viewed 13.9k times · Source

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

Answer

shukshin.ivan picture shukshin.ivan · Mar 14, 2016

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