参数丢失或未作为PHP CURL的JSON数组发送


Parameters missing or not sent as an JSON array with PHP CURL

错误:

{"jsonrpc":"2.0","id":0,"error":{"code":1,"message":"Parameters missing or not sent as an JSON array. "}}
我代码:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: text/plain, */*; q=0.01")); 
curl_setopt($ch, CURLOPT_URL, 'http://example.com/rpc/ClientApi?_session=XX');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('method' => 'AdsApi.giveAdLifeByCategory','params' => '["Ads4Life"]', 'id' => '0')));
curl_setopt($ch, CURLOPT_POST, 1); 
$result = curl_exec($ch);
?>

我正在尝试模仿这个请求:

[{"method":"AdsApi.giveAdLifeByCategory","params":["Ads4Life"],"id":0}]

正确反应:

[{
     "jsonrpc": "2.0",
     "id": 0,
     "result": {
         "status": true,
         "timeStamp": 0
     }
}]

有什么问题吗?

括号设置错误:

... 'AdsApi.giveAdLifeByCategory','params' => '["Ads4Life"]', 'id' => '0')));
                                               ^---       ^--

您的"模拟"示例在其周围有括号,但您将它们嵌入到数组键定义中,因此当生成JSON时,它实际上是:

... "params": "['"Ads4Life'"]" ...

,其中[""]嵌入在字符串中。

应该是

... 'AdsApi.giveAdLifeByCategory','params' => array('Ads4Life'), 'id' => '0')));
                                                   ^^^^^^^^^^