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

时间:2018-09-06 12:05:59

标签: php curl guzzle wunderlist

我正在尝试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函数,该函数只需要标题,就可以了。

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

1 个答案:

答案 0 :(得分:1)

我只需要将“名称”切换为“标题”即可。

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());
}