Doing POST of JSON data using PHP curl. However, every time data is submitted, error message is displayed:
{ "result" : null, "error":"Invalid method", "id": default, "jsonrpc": "2.0"" }
Of course, my first step was to submit data via browser using Live HTTP Headers plugin, so I can see how raw data looks like:
{"jsonrpc":"2.0","method":"get","id":1,"params":["2145359"]}
So I use this code to format my data:
$post_info=json_encode(array("jsonrpc"=>"2.0","method"=>"get","id"=>1,"params"=>array("2145359")), JSON_UNESCAPED_SLASHES)
When I echo my PHP data, it looks identical:
{"jsonrpc":"2.0","method":"get","id":1,"params":["2145359"]}
Then I use this curl function to post data:
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, some_post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_info);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$result=curl_exec($ch);
curl_close($ch);
return $result;
...and "invalid method" error comes. Any suggestions?
http_build_query()instead of json_encode. I forgot that CURL_OPTFIELDS needs a string, not an array.