php - 通过 REST API 将多个观察者添加到 JIRA 问题

标签 php rest jira jira-rest-api

使用 PHP 和 JIRA REST API,我可以通过以下代码向现有问题添加观察者:

$username = 'xxxx';
$password = 'xxxx';
$proxy = 'http://xxxx:8080/';
$url = "http://xxxx/rest/api/2/issue/xxxx/watchers";

$data = 'name1';

$ch = curl_init();

$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($ch);
$ch_error = curl_error($ch);

if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    echo $result;
}
curl_close($ch);

但是,这种方法只允许我添加一个观察者。有没有办法为现有问题添加多个观察者?我试过这样做:

$data = array(
    'name1',
    'name2',
);

但这会导致错误的请求错误。

最佳答案

所以我能解决这个问题的唯一方法就是调用多个 API 来添加观察者。出于某种原因,API 不接受具有多个名称的格式正确的 JSON 调用。如果只是一个名字,JSON 输出只是 "name1",但对于一个数组中的多个名字,它变成 ["name1","name2","name3"](方括号显然是把它扔掉的原因)。

如果有人知道更好的方法,请告诉我,但这就是我最终所做的(我真的认为这是一种解决方法,而不是一个答案):

$username = 'xxxx';
$password = 'xxxx';
$proxy = 'http://xxxx:8080/';
$url = "http://xxxx/rest/api/2/issue/xxxx/watchers";
$data = array(
    'name1',
    'name2',
    'name3'
);
$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

foreach ($data as $key => $user)
{
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($user));
    $result = curl_exec($ch);
}

curl_close($ch);

关于php - 通过 REST API 将多个观察者添加到 JIRA 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680540/

相关文章:

Git:我删除了本地分支,但我仍然可以切换到它。如何永久摆脱它?

Jira 重新加载小工具规范缓存

php - 通过php打开一个新的html页面

java - 在 Postman 中使用 REST Controller 测试 Spring 应用程序

php - 比较数组和数据库表,如果找到任何匹配项,则获取 img url 并插入到 PHP 中

php - 使用 Curl 和 PHP 将多部分 PUT 上传到 REST 端点

C# - 以字节 block 的形式从 Google Drive 下载

python-3.x - 如何在 Python 3.8 中使用 TLS 1.0?

Php 无法识别 PDO_DBLIB 驱动程序

php - 如何用 Amazon S3 替换 PHP imagecopyresampled?