How to get ticket attachment by API (PHP) | Community
Skip to main content

How to get ticket attachment by API (PHP)

  • January 9, 2020
  • 2 replies
  • 0 views

Hi,

I am using Zendesk API from this repository: https://github.com/zendesk/zendesk_api_client_php

It's good so far. I can create ticket, find, update, upload attachment, etc. But when I want to get the ticket attachment, I can't find any method/function from the repo. I think it hasn't been implemented yet. The method clearly stated in https://developer.zendesk.com/rest_api/docs/support/attachments . 

Show Attachment
GET /api/v2/attachments/{id}.json

Then I tried to implement it by adding a new function 'show' in Zendesk/API/Resources/Core/Attachments.php like this:

protected function setUpRoutes()
{
    $this->setRoutes([
        'upload' => "uploads.json",
        'deleteUpload' => "uploads/{token}.json",
        'show' => "attachments/{id}.json",
    ]);
}

public function show($id=null)
{
    $response = Http::send(
        $this->client,
        $this->getRoute(__FUNCTION__, ['id' => $id]),
        ['method' => 'GET']
    );

    return $response;
}

Then I call the function from my php file like this:

...

$attachment = $client->attachments()->show($ticketId);

print_r($attachment);

But I get 404 response {"error":"RecordNotFound","description":"Not found"}.  Actually there is an attachment file in the ticket.

How to do it correctly?

2 replies

  • Author
  • January 9, 2020

Okay. I got the answer from https://develop.zendesk.com/hc/en-us/community/posts/360001643548-How-to-get-attachment-id-from-a-ticket-id-

But I still need to modify Zendesk/API/Resources/Core/Attachments.php to add new function 'comments'. it seems the naming not good and confusing but it's working for me.

protected function setUpRoutes()
{
   $this->setRoutes([
      'upload' => "uploads.json",
      'deleteUpload' => "uploads/{token}.json",
      'show' => "attachments/{id}.json",
      'comments' => "tickets/{id}/comments.json"
   ]);
}

public function comments($id=null)
{
   $response = Http::send(
      $this->client,
      $this->getRoute(__FUNCTION__, ['id' => $id]),
      ['method' => 'GET']
   );

   return $response;
}

Then I call the function like this:
$attachments = $client->attachments()->comments($ticketId);


Greg29
  • January 29, 2020

Thank you for sharing your solution, @Satria Faestha! IF you've moved this into further testing/production, could you let us know if you've run into any problems with this? If so, please let us know to see if we can help.