Having difficulty creating a Task via PHP

I am using following Php CURL base codee to create the task, but it is not working:

$WCURL = ‘https://app.asana.com/api/1.0/tasks’;
$postFields = array (
‘name’ => ‘Buy catnip’,
‘assignee_status’ => ‘upcoming’,
‘completed’ => false,
‘html_notes’ => ‘Mittens really likes the stuff from Humboldt.’,
‘workspace’ => ‘1112458401245755’,
‘projects’ => array (
0 => ‘1112457859980672’,
),
“parent” => null
);

$postFields = json_encode($postFields);

$CURLHTTPHeader = array(
‘Content-Type: application/json’,
‘Accept: application/json’,
‘Authorization: Bearer ---------’
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$WCURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER,$CURLHTTPHeader);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curlResponse = curl_exec ($ch);
curl_close ($ch);

echo ‘

’;
print_r($curlResponse);
echo ‘
’;

Error : “You should specify one of workspace, parent, projects”

Please help me. Thanks in advance!

Hi @Pardeep and welcome to the forum,

I don’t know PHP so I can’t help with any possible PHP syntax issues, but here are some notes:

  1. You need to wrap your fields array inside of a data structure; see the “Body parameter” example here for how that needs to be. I suspect this is the source of your error.

  2. Do not include assignee_status if you are not setting an assignee. Doing so will result in a Bad request error.

  3. If you include html_notes, the string you pass must be considered valid HTML by Asana; a plain text string will result in a Bad request error. In my experience, at a minimum I believe it must be surrounded by a <body></body> tag. Alternately, just use notes instead of html_notes and then a plain text string will be fine.

@Phil_Seeman with all the posts using PHP you answer to, this will soon not be the case anymore :slight_smile: