Unable to create a task through Asana API using PHP, getting error

I am trying to create a task and assign it to the project through API using PHP, but I am getting an error while assigning the task to the project. I am using PHP wrapper class from asana’s official GitHub page, here is the link: GitHub - Asana/php-asana: Official PHP client library for the Asana API v1

<?php require_once('/asana.php'); // See class comments and Asana API for full info $asana = new Asana(array('personalAccessToken' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX')); $workspaceId = 'XXXXXXXXXXXXXXXXXX'; $projectId = 'XXXXXXXXXXXXXXXXXXXX'; // First we create the task $result = $asana->createTask(array( 'workspace' => $workspaceId, 'name' => 'Hello World!', 'assignee' => 'xxx@xxx.com' //'followers' => array('XXXXX', 'XXXXXXXX') // We add some followers to the task... (this time by ID), this is totally optional )); var_dump($result); // As Asana API documentation says, when a task is created, 201 response code is sent back so... var_dump( $asana->getData()->id); $taskId = $asana->getData()->id; // Here we have the id of the task that have been created // Now we do another request to add the task to a project $asana->addProjectToTask($taskId, $projectId); if ($asana->hasError()) { echo 'Error while assigning project to task: ' . $asana->responseCode; } else { echo 'Success to add the task to a project.'; }

Your code is unreadable sorry, can you format it? What is the error?

1 Like

sorry about that.

<?php
require_once('/asana.php');

// See class comments and Asana API for full info
$asana = new Asana(array('personalAccessToken' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX')); 

$workspaceId = 'XXXXXXXXXXXXXXXXXX'; 
$projectId   = 'XXXXXXXXXXXXXXXXXXXX'; 

// First we create the task
$result = $asana->createTask(array(
    'workspace' => $workspaceId, 
    'name'      => 'Hello World!', 
    'assignee'  => 'xxx@xxx.com' 
    //'followers' => array('XXXXX', 'XXXXXXXX') // We add some followers to the task... (this time by ID), this is totally optional
));


var_dump($result);
// As Asana API documentation says, when a task is created, 201 response code is sent back so...
var_dump( $asana->getData()->id);
$taskId = $asana->getData()->id; // Here we have the id of the task that have been created

// Now we do another request to add the task to a project
$asana->addProjectToTask($taskId, $projectId);

if ($asana->hasError()) {
    echo 'Error while assigning project to task: ' . $asana->responseCode;
} else {
    echo 'Success to ad`Preformatted text`d the task to a project.';
}

$taskId = $asana->getData()->id; this line of code giving error, trying to get property of non-object
Error while assigning a project to task: 0

What does var_dump($result);show?

NULL

It is not creating task

You must be getting some kind of error from the server. Asana’s error are always well written and that would help. I don’t know PHP well but isn’t it any way to monitor the server response?

I am getting an error like, invalid request

I believe you might have some details in the error message in this case.

I’m not sure if this is the case but what I see in docs, assignee field seems to be gid and not email.

You can see API details here: Build an app with Asana

Thanks @Dmytro_TOLSTYI1

Yes, I figured out it. I have doubt @Bastien_Siebman, can we modify task details using asana API?

Yes you can do almost anything with the API.