【问题标题】:How to pass array of arguments to cURL in command line?如何在命令行中将参数数组传递给 cURL?
【发布时间】:2016-06-25 02:30:55
【问题描述】:

当我从我的 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);       

它工作正常。我想在命令行中执行相同的请求。首先我对数组进行 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  

然后我得到了错误。

错误:无法解析请求正文

有没有办法解决这个问题?

UPDATE:此处提供的 URL 是一个虚拟值,实际上我正在尝试连接 Chargify API(定期计费解决方案)。

【问题讨论】:

  • -H Content-Type:application/json 你为什么这么说?
  • 因为我正在尝试发送 json 数据
  • product[name]=nw&product[handle]… 看起来不像 JSON
  • 那我该如何解决呢?

标签: php curl command-line terminal chargify


【解决方案1】:

我认为您应该将数据放在单引号内

curl ... --data '这里有一些数据' ...

编辑:

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 是文件数组的情况下使用逗号分隔的文件名。

【讨论】:

  • 我已经检查过了。但没有变化
  • 我已经检查过了。但没有变化
【解决方案2】:

您的服务器似乎确实接受了 json 有效负载发布数据。你可能忘了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)
  ...

【讨论】:

  • {"errors":["Name: cannot be blank.","Interval unit: must be 'month' or 'day'.","Recurring Interval: cannot be blank.","Price: cannot be blank. Enter '0' if free."]}这是出局
  • 嗯,看起来curl 现在发送了正确的json 请求,但是存在一些逻辑问题。我可以假设[product[name]]应该在命令中替换为product[name]
  • @ARUNBALANNV 好吧,这取决于你,阅读你提交数据的服务的文档。猜测所需数据的结构超出了您最初问题的范围。
  • 我最后一次尝试是设置以下数据结构:{"product":{"name": ... "handle":...}}。我已经更新了答案
  • {"status":"500","error":"Internal Server Error"} 这是最新出的
【解决方案3】:

最后我可以通过拆分数组参数来解决这个问题。我的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 2021-02-25
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2020-08-02
    相关资源
    最近更新 更多