Search code examples
phpapijson-rpc

php api Unauthorized request json rpc 2.0


I want to access api with my api key that is correct but still geting back error 401 unauthorized. Api using json rpc 2.0.

This is example request from api docs.

Request:
{
  "jsonrpc": "2.0",
  "method": "getBoards",
  "params": {
       "clientId": "apikey"
 },
 "id": 1
}

And this is my php code.

$apiKey = 'apikey';
$apiUrl = 'apiUrl';

$data =  array(
        "jsonrpc" => "2.0",
        "method" => "getBoards",
        "params" => array(
            "clientId" => $apiKey
        ),
        "id" => "1"
);

$data_string = json_encode($data);
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

$response = curl_exec($ch);
curl_close($ch);

print_r($response);

Solution

  • Okey my client made a mistake and api key was wrong so at least this is the correct way how to use php with protocol jsonrpc 2.0