Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wunderlist Tasks - how do I get the tasks for a list using curl in php

I have been searching quite a bit now and can't get an overview of Tasks from a single list using the Wunderlist endpoint: https://a.wunderlist.com/api/v1/tasks

I can get Lists, Folders and create List, Folders, Tasks so that works fine. But how do I get the Tasks from a list? I tried to interpret the documentation found here: https://developer.wunderlist.com/documentation/endpoints/task

When I do a GET method I get this error message:

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

However, I don't want to add a title, since I don't want to create a new task, I just want the tasks of that list back.

What am I doing wrong here?

Here is my code so far:

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 --- I have also tried these variants in case anyone wonders. They all give the same error "bad_request"

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"} 
like image 301
Teeuwis Hillebrand Avatar asked Jun 25 '15 11:06

Teeuwis Hillebrand


1 Answers

As pointed out by Rotem, you're using adding the $parameters array as CURLOPT_POSTFIELDS. As the name suggests this is meant for http POST requests only.

Putting the parameters as query params in the $url and passing null for the query params made your example work for me.

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

Note: I did use a list_id I have access to instead of using 123

like image 96
schlubbi Avatar answered Nov 17 '22 02:11

schlubbi