0

I'm trying to form a JSON-RPC request in the following format:

{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "call.search",
    "params": [
        "acc",
        srh: {
            "user"
        }
    ]
}

But I'm having difficulty creating the request with the correct format using PHP. My code looks like this:

    function prepareRequest($procedure, array $params = array())
{
    $payload = array(
        'jsonrpc' => '2.0',
        'method' => $procedure,
        'id' => mt_rand()
    );

    if (! empty($params)) {
        $payload['params'] = $params;
    }

    return $payload;
}
$req = json_encode('9999');

$request = prepareRequest('call.search', array('acc','srh' => $req));

$json = json_encode($request);

echo $json;

My result so far looks like this:

    {
      "jsonrpc":"2.0",
      "method":"call.search",
   "id":1339580122,
   "params":{
      "0":"acc",
      "srh":"\"user\""
   }
}
xVGERx
  • 41
  • 6
  • 1
    Don't `json_encode` the data that you're later going to `json_encode`! That's why your data is double escaped. – deceze Feb 08 '15 at 01:08
  • It looks like the json is nested, that's why I tried json encoding the data. `$request = prepareRequest('call.search', array('acc', 'srh' => 'grpid'));` Doesn't work either – xVGERx Feb 08 '15 at 02:05
  • What does "doesn't work" mean? What do you get? – deceze Feb 08 '15 at 02:44
  • I have tried multiple variations to try and match the request syntax given in the Marchex Call Analytics API documentation and none have been successful. The last one produces this `{ "jsonrpc":"2.0", "id":105083196, "method":"call.search", "params":{ "0":"acc", "srh":"grpid" } }` – xVGERx Feb 08 '15 at 03:39
  • 1
    Yeah, putting it that way... What you expect to get is impossible; it's not valid JSON. – deceze Feb 08 '15 at 04:41
  • That's what I thought, perhaps it's an error in docs. This is the one I really need `{ "jsonrpc": "2.0", "method": "call.search", "params": [ "QrOXxEE9-fATtgAD", { "start": "2013-12-06T9:00:00Z", "end": "2013-12-06T17:00Z", "extended": true } ], "id": 1 }` – xVGERx Feb 08 '15 at 05:20

0 Answers0