奇妙清单任务 - 如何使用php中的curl获取列表的任务

时间:2015-06-25 11:40:54

标签: php api curl wunderlist

我现在已经搜索了很多,并且无法使用Wunderlist端点从单个列表中了解任务:https://a.wunderlist.com/api/v1/tasks

我可以获取列表,文件夹并创建列表,文件夹,任务以便正常工作。但是如何从列表中获取任务?我试着解释这里的文档:https://developer.wunderlist.com/documentation/endpoints/task

当我执行GET方法时,我收到以下错误消息:

{"error":{"type":"missing_parameter","translation_key":"api_error_missing_params","message":"Missing parameter.","title":["required"]}}

但是,我不想添加标题,因为我不想创建新任务,我只想恢复该列表的任务。

我在这里做错了什么?

到目前为止,这是我的代码:

function doCall($endPoint, $parameters = array(), $method = 'GET')
{
    // check if curl is available
    if (!function_exists('curl_init')) {
        print "This method requires cURL (http://php.net/curl), it seems like the extension isn't installed. ".__LINE__;
        exit();
    }
    //prepare content
    $parametersdata = json_encode($parameters);

    // define url
    $url = 'https://a.wunderlist.com/api/v1/' . $endPoint;

    // init curl
    $curl = curl_init();
    // set options
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    // init headers
    $headers = array();

    // add to header
    $headers[] = 'X-Access-Token: XXX';
    $headers[] = 'X-Client-ID: XXX';

    // method is POST, used for login or inserts
    if ($method == 'POST') {
        // define post method
        curl_setopt($curl, CURLOPT_POST, 1);
    // method is DELETE
    } elseif ($method == 'DELETE') {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
    } else {
        curl_setopt($curl, CURLOPT_HTTPGET, 1);
    }

    // parameters are set
    if (!empty($parameters)) {
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Content-Length: ' . strlen($parametersdata);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $parametersdata );
    }
    // define headers with the request
    if (!empty($headers)) {
        // add headers
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    // execute
    $response = curl_exec($curl);
    // debug is on
    if (true) {
        echo $method." ".$url . '<br/><pre>';
        print"\n--headers--\n";
        print_r($headers);
        print"\n--parameters--\n";
        print_r($parameters);
        print"\n--parametersdata--\n";
        print_r($parametersdata);
        print"\n--response--\n";
        print_r($response);
        echo '</pre><br/><br/>';
    }
    // get HTTP response code
    $httpCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
    // close
    curl_close($curl);
    // response is empty or false
    if (empty($response)) {
        //throw new Exception('Error: ' . $response);
        print "Error: ". $response." ".__LINE__;
    }
    // init result
    $result = false;
    // successfull response
    if (($httpCode == 200) || ($httpCode == 201)) {
        $result = json_decode($response, true);
    }
    // return
    return $result;
}

$listid = 123;
$url = 'tasks';
$tasks = doCall($url,array('list_id' => $listid), 'GET');
echo $tasks;

- EDIT ADDENDUM --- 我也尝试过这些变种以防万一。他们都给出了同样的错误&#34; bad_request&#34;

GET a.wunderlist.com/api/v1/tasks{"list_id":"141329591"}
GET a.wunderlist.com/api/v1/tasks{"list_id":141329591}
GET a.wunderlist.com/api/v1/tasks/{"list_id":"141329591"} 

1 个答案:

答案 0 :(得分:8)

正如Rotem所指出的,您正在使用$parameters数组添加CURLOPT_POSTFIELDS。顾名思义,这仅适用于http POST请求。

将参数作为查询参数放在$url中,并将null传递给查询参数使我的示例正常工作。

$listid = 123;
$url = 'tasks?list_id=123';
$tasks = doCall($url,null,'GET');
echo $tasks;

注意:我确实使用了我有权访问的list_id,而不是使用123