将数据发布到AtTask的API?

时间:2014-01-02 20:24:28

标签: rest curl attask

我正在使用AtTask的API和PHP和cURL。

有没有办法发布数据而不是用问号将其附加到网址的末尾?

我知道我可以像CURLOPT_CUSTOMREQUEST => 'POST'一样更改请求名称,我尝试添加CURLOPT_POST => true

但是,CURLOPT_POSTFIELDS => array('name' => 'Untitled Project')仍然被忽略。

有没有人使用过它?

2 个答案:

答案 0 :(得分:0)

非常确定你需要使用http_build_query()包装你的postfields数组。这是一个适合我的登录示例(当我输入我的用户名和密码时):

$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);

希望有所帮助。

答案 1 :(得分:0)

我遇到了你遇到的同样问题。你问这个问题已经有几个月了,但是如果你还在遇到这个问题(或者其他任何人遇到这个问题),问题是你需要为POST / GET设置正确的CURL选项。

对于GET,您需要将CURLOPT_HTTPGET设置为“true”。它只是确保标题符合AtTask API服务器的正确顺序。

我刚刚为我们对他们的示例StreamClient类所做的更改创建了一个github repo。

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

随意使用它或只是从中提取代码。对于您的登录示例,GreenJimmy的示例是100%准确的。对于您的搜索问题,您需要执行以下操作。

$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);

我的github repo中的StreamClient允许将数组用于搜索参数和响应字段。它确实让事情变得容易多了。例如。

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);