4

I'm using Zendesk php class, and the following function is used to delete attachments.

/**
 * Delete one or more attachments by token or id
 * $params must include one of these:
 *        'token' - the token given to you after the original upload
 *        'id' - the id of the attachment
 *
 * @param array $params
 *
 * @throws MissingParametersException
 * @throws ResponseException
 * @throws \Exception
 *
 * @return bool
 */
public function delete(array $params) {
    if(!$this->hasAnyKey($params, array('id', 'token'))) {
        throw new MissingParametersException(__METHOD__, array('id', 'token'));
    }
    $endPoint = Http::prepare(($params['token'] ? 'uploads/'.$params['token'] : 'attachments/'.$params['id']).'.json');
    $response = Http::send($this->client, $endPoint, null, 'DELETE');
    if ($this->client->getDebug()->lastResponseCode != 200) {
        throw new ResponseException(__METHOD__);
    }
    $this->client->setSideload(null);
    return true;
}

According to the comments, either the token or ID is required when running this function.

I have tried using an id

$attachment = $client->attachments()->delete(array('id'=>'1187146218','token'));

However it keeps trowing exception

  PHP Notice:  Undefined index: token in /home/adam/web/srv11/public_html/vendor/zendesk/zendesk_api_client_php/src/Zendesk/API/Attachments.php on line 106
PHP Fatal error:  Uncaught exception 'Zendesk\API\ResponseException' with message 'Response to Zendesk\API\Attachments::delete is not valid. Call $client->getDebug() for details' in /home/adam/web/srv11/public_html/vendor/zendesk/zendesk_api_client_php/src/Zendesk/API/Attachments.php:109
Stack trace:
#0 /home/adam/web/srv11/public_html/functions/support-attachment-delete.php(15): Zendesk\API\Attachments->delete(Array)
#1 {main}
  thrown in /home/adam/web/srv11/public_html/vendor/zendesk/zendesk_api_client_php/src/Zendesk/API/Attachments.php on line 109

your help is highly appreciated

1
  • either token or id is required....you are passing an array with 2 indexes but only 1 value. You could try removing ,'token' or changing it to array('id'=>'1187146218','token'=>null) But I think the undefined index is that token peice. Commented May 28, 2015 at 20:30

1 Answer 1

2

the way you have this written

array('id'=>'1187146218','token')

actually gives an array like this:

array('id'=>'1187146218', 0 => 'token')

so there is no 'token' index. This should work if you just change to

$attachment = $client->attachments()->delete(array('id'=>'1187146218','token' => NULL));
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.