php - 使用 Jira REST API

标签 php json rest curl jira-rest-api

我想使用 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);

当我运行它时,没有任何反应。我没有得到任何反馈。

我找到了 here , 并将信息替换为我的有效信息。

最佳答案

首先定义一些有用的全局变量来帮助:

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 可能需要稍微配置

关于php - 使用 Jira REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35747309/

相关文章:

iOS 9 - 在另一台计算机上的网络浏览器中打开资源,由平板电脑触发

php - 以 Yii 多模型形式获取第二个模型值时出错

javascript - 使用 NodeJS(或 PHP)将 API 数据保存到 JSON 文件

arrays - PySpark - RDD 到 JSON

ios - TableView 不显示数据

javascript - 如何查询 Firebase 的 equalTo bool 参数?

java - 导出 CSV 文件 - JAX RS - REST - AJAX

spring - STS中整个Spring项目的UML图(Class Diagram)

php - mySQL `date_updated` 列仅适用于特定字段 : SELECT and INSERT vs TRIGGER?

php - 使用 PHP SDK 将可选参数发送到 Twilio Studio 以触发 Flow