PHP提示:未定义的索引

4
我正在使用Zendesk PHP类,下面的函数用于删除附件。
/**
 * 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;
}

根据评论,运行此函数时需要令牌或ID。 我尝试使用了一个ID。
$attachment = $client->attachments()->delete(array('id'=>'1187146218','token'));

然而它不断抛出异常。
  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


要么需要token,要么需要id...你正在传递一个包含2个索引但只有1个值的数组。你可以尝试删除 ,'token' 或将其更改为 array('id'=>'1187146218','token'=>null) 但我认为未定义的索引是那个token部分。 - RightClick
1个回答

2
你写的方式
array('id'=>'1187146218','token')

实际上会得到这样一个数组:
array('id'=>'1187146218', 0 => 'token')

因此,没有“token”索引。如果您只更改为,则应该有效。
$attachment = $client->attachments()->delete(array('id'=>'1187146218','token' => NULL));

你能看到这个吗?https://stackoverflow.com/questions/48295877/webclient-too-many-automatic-redirections-were-attempted - user9046719

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接