POST data to AtTask's API?

606 Views Asked by At

I'm using AtTask's API with PHP and cURL.

Is there a way to POST data instead of appending it to the end of the URL with a question mark?

I know that I can change the request name itself like CURLOPT_CUSTOMREQUEST => 'POST' and I tried adding CURLOPT_POST => true

However, the CURLOPT_POSTFIELDS => array('name' => 'Untitled Project') is still ignored.

Did anyone work with this?

2

There are 2 best solutions below

1
On

Pretty sure you need to wrap your postfields array with http_build_query(). Here is an example for logging in that works for me (when I put in my username and password):

$URL = 'https://hub.attask.com/attask/api/login';
$username = 'admin';
$password = 'user';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
    'username'=>$username,
    'password'=>$password
)));
$results = curl_exec($ch);
curl_close($ch);

print_r($results);

Hope that helps.

0
On

I just ran into the same problem you did. It's been a few months since you asked the question, but if you're still running into it (or for anyone else hitting this wall), the issue is that you need to set the proper CURL options for POST/GET.

For a GET you'll need to set CURLOPT_HTTPGET to "true". It just makes sure the headers are in the correct order for the AtTask API server.

I've just created a github repo for the changes I've made to their sample StreamClient class.

https://github.com/angrychimp/php-attask

Feel free to use that or just lift the code from it. For your login example, GreenJimmy's example is 100% accurate. For your search question, you'll need to do something like the following.

$URL = 'https://hub.attask.com/attask/api/v4.0/';
$username = 'admin';
$password = 'pass';

$URL .= "task/search/?sessionID=$sessionID&ID=$taskID";

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$results = curl_exec($ch);
curl_close($ch);

print_r($results);

The StreamClient in my github repo allows for arrays to be used for search params and response fields. It does make things a lot easier. For example.

require_once('StreamClient.php');
$client = new StreamClient('https://hub.attask.com', '4.0');
$client->login('admin', 'pass');
$records = $client->search('task', array('ID' => $taskID), array('assignedToID:emailAddr','actualWork'));
var_dump($records);