Search code examples
phpcurltodoist

PHP Curl / Todoist / Adding Item


I am trying to add an item with the todoist API using PHP Curl according to this:

https://developer.todoist.com/?shell#add-an-item

It quotes this code:

$ curl https://todoist.com/API/v6/sync -X POST \
    -d token=0123456789abcdef0123456789abcdef01234567 \
    -d commands='[{"type": "item_add", "temp_id": "43f7ed23-a038-46b5-b2c9-4abda9097ffa", "uuid": "997d4b43-55f1-48a9-9e66-de5785dfd69b", "args": {"content": "Task1", "project_id": 128501470}}]'

I am trying this in PHP:

$args = '{"content": "Task1", "project_id":'.$project_id.'}';
    $url = "https://todoist.com/API/v6/sync";
    $post_data = array (
        "token" => $token,
        "type" => "item_add",
        "args" => $args,
    );

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

    $output = curl_exec($ch);

    curl_close($ch);

So I have the token, the args, the type but I can't seem to get it to work.

What would the PHP equivalent of that call be?


Solution

  • Try this:

    $url = "https://todoist.com/API/v6/sync";
    $post_data = [
        'token' => $token,
        'commands' => 
            '[{"type": "item_add", ' .
            '"temp_id": "43f7ed23-a038-46b5-b2c9-4abda9097ffa", ' .
            '"uuid": "997d4b43-55f1-48a9-9e66-de5785dfd69b", ' . 
            '"args": {"content": "Task1", "project_id":'.$project_id.'}}]'
    ];
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    curl_setopt($ch, CURLOPT_POST, 1);
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    
    $output = curl_exec($ch);
    
    curl_close($ch);
    

    I haven't tested it, but I'm pretty sure this is the equivalent curl command implemented in PHP. Let me know how it works out.