如何在命令行中将参数数组传递给 cURL?
How to pass array of arguments to cURL in command line?
当我从我的 PHP 脚本发送 cURL 请求时,我得到了想要的响应。
我的要求是这样的。
$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);
它工作正常。我想在命令 line.First 中执行相同的请求,我 json 对数组进行了编码,我尝试使用此命令
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
然后我得到了错误。
Error: Unable to parse request body
有什么办法可以解决吗?
UPDATE : The URL provided here is a dummy value,Actually i am trying to connect with Chargify API (Recurring billing solution ).
我认为你应该将数据放在单引号内
curl ... --data 'some data here' ...
编辑:
ON WINDOWS
通过 cURL 传递数组参数的正确方法如下所示:
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
查看使用逗号分隔的文件名,其中服务器期望 files
是一个文件数组。
您的服务器似乎确实接受 json 负载 post 数据。您可能忘记了 json_decode
您的数据,这是解决方法:
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
如果我将它发送到我的 php 脚本 <?php var_dump(json_decode(file_get_contents('php://input')));
我会看到正确答案:
object(stdClass)#1 (1) {
["product"]=>
object(stdClass)#2 (8) {
["name"]=> string(2) "nw"
["handle"]=> int(150)
...
最后我可以通过拆分数组参数来解决这个问题。我的 cURL 命令是这样的。
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
当我从我的 PHP 脚本发送 cURL 请求时,我得到了想要的响应。
我的要求是这样的。
$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);
它工作正常。我想在命令 line.First 中执行相同的请求,我 json 对数组进行了编码,我尝试使用此命令
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
然后我得到了错误。
Error: Unable to parse request body
有什么办法可以解决吗?
UPDATE : The URL provided here is a dummy value,Actually i am trying to connect with Chargify API (Recurring billing solution ).
我认为你应该将数据放在单引号内
curl ... --data 'some data here' ...
编辑:
ON WINDOWS
通过 cURL 传递数组参数的正确方法如下所示:
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
查看使用逗号分隔的文件名,其中服务器期望 files
是一个文件数组。
您的服务器似乎确实接受 json 负载 post 数据。您可能忘记了 json_decode
您的数据,这是解决方法:
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
如果我将它发送到我的 php 脚本 <?php var_dump(json_decode(file_get_contents('php://input')));
我会看到正确答案:
object(stdClass)#1 (1) {
["product"]=>
object(stdClass)#2 (8) {
["name"]=> string(2) "nw"
["handle"]=> int(150)
...
最后我可以通过拆分数组参数来解决这个问题。我的 cURL 命令是这样的。
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