2015-11-12 174 views
1

添加多个观察家JIRA问题使用PHP和JIRA REST API,我可以通过这个代码添加观察家对现有问题:通过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', 
); 

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

回答

0

所以我可以解决这个问题的唯一方法就是多个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);