Unauthorize authentification error HTTP 401 with RT REST 2 API

Hello,
I try to create ticket with curl in php but i always have this issue :

HTTP/1.1 401 Unauthorized Date: Sun, 03 Jan 2021 13:09:56 GMT Server: Plack::Handler::Starlet Content-Length: 26 Content-Type: application/json; charset=utf-8 Connection: close {“message”:“Unauthorized”}

This is my code
$ticket_data=array(
“Queue” => “General”,
“Subject” => “hello world 2021”,
“Content” => " my test content",
“ContentType”=> “text/html”,
//“CustomFields”=>$files
);

$post = $ticket_data; //array(‘val1’ => ‘value’,‘val2’ => ‘value’,‘CustomFields’=> $files );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$base_url.‘ticket/’);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HEADER, array(‘Content-Type: application/json’,‘Authorization: token 1-14-c6cde7f274dcbf4f8d1bf2da32449518’));
//curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result=curl_exec ($ch);
$resp=json_decode($result);
echo curl_error($ch);
var_dump($resp);
curl_close ($ch);

Please help thanks

Actually it looks like there is a bug in your code:

curl_setopt($ch, CURLOPT_HEADER, array(‘Content-Type: application/json’,‘Authorization: token 1-14-c6cde7f274dcbf4f8d1bf2da32449518’));

CURLOPT_HEADER should be CURLOPT_HTTPHEADER and should include the media type, so this:

$headers = array(
  "Content-type: application/json",
  "Authorization: token 1-14-c6cde7f274dcbf4f8d1bf2da32449518"
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Thanks @knation for your quick reaction. It work now

Hi I’m having the same issue using curl here is what I’m getting

curl -H “Content-Type: application/json” -H ‘Authorization: token 1-12-fe2beae4ddac346b21033fe44eff0035’ https://irf.xxxx.com/REST/2.0/ticket/66
{“message”:“Unauthorized”}
Error code: 500

Do you have this set?
https://docs.bestpractical.com/rt/5.0.3/web_deployment.html#Token-Authentication