Starx
Starx

Reputation: 79011

Posting task on a particular project via CURL

I seem to have got stuck trying to use the Asana API. I am trying to post a task on a particular project.

Here is what I am trying:

$api = 'myapikey';
$url = 'https://app.asana.com/api/1.0/tasks';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Don't print the result
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Don't verify SSL connection
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //         ""           ""
curl_setopt($curl, CURLOPT_USERPWD, $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$data = array(
    "data" => array(
        "workspace" => "workspace id",
        "name" => "Task Name",
        "notes" => "notes",
        "assignee" => "assignee id"
    )
);
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        
$html = curl_exec($curl);
curl_close($curl);

$html = json_decode($html);
var_dump($html);

It didn't work, It creates the task on Undefined Project. I tried with the following variations:

  1. $url = 'https://app.asana.com/api/1.0/projects/4649161839339/tasks';

  2. $url = 'https://app.asana.com/api/1.0/tasks/projects/4649161839339';

Any Idea?

Upvotes: 1

Views: 236

Answers (1)

Greg S
Greg S

Reputation: 2079

You can add projects to tasks at creation time by specifying a projects field, which consists of an array of project IDs to add the task to, i.e.: "projects" => [4649161839339],. This should be covered in the developer documentation.

For PHP

$data = array(
    "data" => array(
        "workspace" => "workspace id",
        "projects" => array('project_id'),
        "name" => "Task Name",
        "notes" => "notes",
        "assignee" => "assignee id"
    )
);

Once the task is created, you can use the /tasks/ID/addProject and /tasks/ID/removeProject endpoints to add and remove projects.

Upvotes: 2

Related Questions