19 April 2022

You can't directly add a file to a ticket, but y can upload a file and attach it to a ticket comment. The attachment appears in the ticket comment. You can only attach the uploaded file to a ticket comment with the Tickets API when adding the comment to a ticket you're creating or updating. To make things easier we use an existing php library.

Official API documentation: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket-att...

Source code viewer
  1. // https://github.com/zendesk/zendesk_api_client_php
  2. use Zendesk\API\HttpClient as ZendeskAPI;
  3.  
  4. define('ZENDESK_SUBDOMAIN', '');
  5. define('ZENDESK_USERNAME', '');
  6. define('ZENDESK_TOKEN', '');
  7. define('ZENDESK_SUBDOMAIN', '');
  8.  
  9. $client = new ZendeskAPI(ZENDESK_SUBDOMAIN);
  10. $client->setAuth('basic', ['username' => ZENDESK_USERNAME, 'token' => ZENDESK_TOKEN]);
  11. $domain = 'https://' . ZENDESK_SUBDOMAIN . '.zendesk.com/';
  12.  
  13. function putFile($path, $type, $name) {
  14. $attachment = $client->attachments()->upload([
  15. 'file' => $path,
  16. 'type' => $type,
  17. 'name' => $name,
  18. ]);
  19.  
  20. return $attachment->upload->token;
  21. }
  22.  
  23.  
  24. $client->tickets()->create([
  25. // 'subject' => $data['subject'],
  26. // 'requester' => [
  27. // 'locale' => $data['language'] === 'en' ? 'en-US' : $data['language'],
  28. // 'name' => $data['name'],
  29. // 'email' => $data['email'],
  30. // 'phone' => $data['phone'],
  31. // 'user_fields' => [
  32. // ...
  33. // ],
  34. // ],
  35. // 'description' => $data['description'],
  36. // 'comment' => [
  37. // 'public' => false,
  38. // 'html_body' => $data['comment'],
  39. 'uploads' => [ putFile($path, $type, $name) ],
  40. // ],
  41. // 'group_id' => $data['group_id'],
  42. // 'custom_fields' => $custom_fields,
  43. // 'priority' => 'normal',
  44. ]);
Programming Language: PHP