Freshdesk API: Attach dataurl as file using PHP and CURL

336 Views Asked by At

I'm trying to create a ticket in Freshdesk with a screenshot as an attachment. Screenshot is captured in a canvas and converted to dataurl. And I'm using the following code to pass it to the Freshdesk API (V2)

$data = [
"description" => $messageBody,
"subject" => 'Bug Report from Client',
"email" => $replyTo,
"priority" => 1,
"status" => 2,
"attachments" => [
    [
        "type" => "file",
        "name" => "Screenshot",
        "content-type" => "image/png",
        "resource" => $dataUrl,
    ]
]
];

$url = "https://domain.freshdesk.com/api/v2/tickets";
$apiKey = "key";
$headers = array(
    'Content-Type:application/json',
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey);
$resultStr = curl_exec($ch);

It results in the following error.

{
 field: "attachments", 
 message: "It should contain elements of type valid file format only", 
 code: "datatype_mismatch" 
}

Without the attachments field, the above code works fine. How do I fix this?

0

There are 0 best solutions below