消费Jira REST API

7

我想调用Jira的REST API,但一直无法成功。

Jira网站提供了以下请求URL:

curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/ 

这是一个json数组,存储在名为collector.json的文件中:

"fields": [
            {
                "project":
                        {
                            "key": "ATL"
                        },
                "summary": "REST ye merry TEST.",
                "description": "Creating of an issue using project keys and issue type names using the REST API",
                "issuetype": {
                                "name": "Task"
                             }
            }
        ]

以下是代码:

<?php
include_once "collector.php";

$jiraValues = jsonArray("collector.json");
$jira_url = "http://jira.howareyou.org:8091/rest/api/2/issue/createmeta";
$jiraString = json_encode($jiraValues);

$request = curl_init($jira_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($request, CURLOPT_ENCODING, "");
curl_setopt($request, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $jiraString); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
curl_setopt($request, CURLOPT_URL, $jira_url);

$json_raw = curl_exec($request); // execute curl post and store results in $json_raw

curl_close($request); // close curl object

// This line takes the response and breaks it into an array using the specified delimiting character
$jira_response = json_decode($json_raw, TRUE);

print_r($jira_response);

当我运行它时,没有任何反应,也没有任何反馈。
我在这里找到了它(链接),并使用我的有效信息替换了其中的信息。

你从 cURL 请求中获得了响应吗?此外,为了调试,请尝试打印 $json_raw - Pogrindis
为什么你在使用CURLOPT_POSTFIELDS?这个请求应该是GET。 - Dmytro
@Dmitry,你有没有一个我可以使用的例子?适合初学者。 - kya
@Dmitry 让我试试。谢谢 - kya
你发布的CURL命令对你有效吗? - jannis
显示剩余8条评论
1个回答

1
首先定义一些有用的全局变量来帮助:
define('JIRA_URL', 'http://jira.howareyou.org:8091');
define('USERNAME', '');
define('PASSWORD', '');

然后我们需要定义一个 POST 方法:
function post_issue($data) {
    $jdata = json_encode($data);
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_POST => 1,
        CURLOPT_URL => JIRA_URL . '/rest/api/2/issue/createmeta' . $resource,
        CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
        CURLOPT_POSTFIELDS => $jdata,
        CURLOPT_HTTPHEADER => array('Content-type: application/json'),
        CURLOPT_RETURNTRANSFER => true
    ));
    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result);
}

然后我们想要使用它的方式如下:
创建一个新问题。
$new_issue = array(
    'fields' => array(
        'project' => array('key' => 'TIS'),
        'summary' => 'Test via REST',
        'description' => 'Description of issue goes here.',
        'priority' => array('name' => 'Blocker'),
        'issuetype' => array('name' => 'Task'),
        'labels' => array('a','b')
    )
);

调用我们之前创建的函数,将新问题传递进去:
$result = post_issue($new_issue);
if (property_exists($result, 'errors')) {
    echo "Error(s) creating issue:\n";
    var_dump($result);
} else {
    echo "New issue created at " . JIRA_URL ."/browse/{$result->key}\n";
}

通过REST发布新问题的基本示例
注意:Jira API URL可能需要稍作配置。

数据将是JSON数组吗? - kya
@kya 是的,确实更新了!如果您正在传递直接的 JSON 数组,请不要使用 $new_issue 变量。 - Pogrindis
我现在遇到以下错误:在这一行上进行了数组转换,CURLOPT_URL => JIRA_URL . '/rest/api/2/issue/createmeta' . $resource,并且出现以下错误:警告:第一个参数必须是对象或现有类的名称,在此行上:if (property_exists($result, 'errors'))。 - kya
@kya,在post_issue函数的输入中删除 $resource,并且由于您正在直接传入JSON,所以在post_issue函数中无需重新编码它。 - Pogrindis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接