7

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

然后我得到了错误。

错误:无法解析请求正文

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

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

4

3 回答 3

6

您的服务器似乎确实接受了 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)
  ...
于 2016-03-14T10:33:07.210 回答
4

最后我可以通过拆分数组参数来解决这个问题。我的 卷曲命令是这样的。

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
于 2016-03-15T06:43:30.543 回答
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请参阅在服务器期望是文件数组的情况下使用逗号分隔的文件名。

于 2016-03-14T08:40:50.410 回答