'PHP Curl - put data to freshdesk custom field

I want to update my custom field in freshdesk with php curl. Bash command looks like this and it works:

curl -v -u myapikey:X -H "Content-Type: application/json" -X PUT -d '{ "custom_fields": {"hours":"2" }}' 'https://myfreshdeskname.freshdesk.com/api/v2/tickets/1000'

Now im trying to create function in php that will do this for me. For now it looks like that, but it doesn't work and i don't know why:

function freshdesk($url, $apitoken) { 
$data = json_encode(['custom_fields' => ['hours' => '2' ]]); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_USERPWD, $apitoken . ':X');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
curl_close($ch);

return $result;}


Solution 1:[1]

I found what i was missing. It was a header. So i had to add smth like this to code above:

$header = "Content-Type: application/json";

And then:

curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));

Im leaving it here in case someone find it helpfull.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Heylo