【问题标题】:Wunderlist’s API with Guzzle - Create Task - Bad Request 400奇妙清单的 API 与 Guzzle - 创建任务 - 错误请求 400
【发布时间】:2018-09-06 12:05:59
【问题描述】:

我正在试用 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

【问题讨论】:

    标签: php curl guzzle wunderlist


    【解决方案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());
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-18
      • 1970-01-01
      • 2017-04-10
      • 2018-12-06
      • 2019-09-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      相关资源
      最近更新 更多