Wunderlist的API与Guzzle - 创建任务 - 错误请求400

问题描述 投票:1回答:1

我正在尝试Wunderlist API并一直关注这个helpful tutorial。他们还提供demo on Github。它适用于GET调用(和PATCH),但是当我尝试使用POST请求创建新任务时,它返回错误请求,客户端错误:400。

WunderlistClient.php

public function createTask($name, $list_id, $task = []) {
    if (!is_numeric($list_id)) {
        throw new \InvalidArgumentException('The list id must be numeric.');
    }
    $task['name'] = $name;
    $task['list_id'] = $list_id;
    $response = $this->client->post('tasks', ['body' => json_encode($task)]);
    $this->checkResponseStatusCode($response, 201);
    return json_decode($response->getBody());
}

的index.php

try {
    $created = $wunderlist->createTask('New Task', $list_id);
    dump($created);
}
catch(\Exception $exception) {
    dump($exception);
}

我添加了一个createList函数,它只需要title,并且确实有效。

public function createList($title, $list = []) {
    $list['title'] = $title;
    $response = $this->client->post('lists', ['body' => json_encode($list)]);
    $this->checkResponseStatusCode($response, 201);
    return json_decode($response->getBody());
}

为什么我无法创建tasks

php curl guzzle wunderlist
1个回答
1
投票

我只需要将'name'切换为'title'。

public function createTask($title, $list_id, $task = []) {
    if (!is_numeric($list_id)) {
        throw new \InvalidArgumentException('The list id must be numeric.');
    }
    $task['title'] = $title;
    $task['list_id'] = $list_id;
    $response = $this->client->post('tasks', ['body' => json_encode($task)]);
    $this->checkResponseStatusCode($response, 201);
    return json_decode($response->getBody());
}
© www.soinside.com 2019 - 2024. All rights reserved.