使用Jira php API创建问题

时间:2015-08-09 18:40:56

标签: php api rest jira

我正在尝试使用jira rest api创建问题,这是我的代码:

 $new_issue = array(
            'fields' => array(
                'project' => array('key' => "KEY"),
                'summary' => $this->Summary,
                'description' => $this->Description,
                'issuetype' => array('name' => 'Bug')
            )
        );

        $body = json_encode($new_issue);

 self::$handle = curl_init();

 curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
 curl_setopt_array(self::$handle, array(
    CURLOPT_URL => "jiraUrl//rest/api/2/issue/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_HTTPHEADER => array("content-type:application/json"),
    CURLOPT_HEADER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_ENCODING => ''
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => $username . ':' . $password
      ));


     $response   = curl_exec(self::$handle);
            $error      = curl_error(self::$handle);

我收到此错误: {“errorMessages”:[“由于输入结束而没有要映射到Object的内容”]} 有什么建议吗?

3 个答案:

答案 0 :(得分:0)

此处缺少收盘价:

CURLOPT_URL => "jiraUrl//rest/api/2/issue/

jiraUrl可能需要像$jiraUrl

所以,我的看法:

CURLOPT_URL => "$jiraUrl//rest/api/2/issue/",

答案 1 :(得分:0)

对于仍然想知道如何使用JIRA REST API创建问题的人,下面的最小代码对我有用:

$url = "http://your.domain.here/rest/api/latest/issue/"
$username = "username";
$password = "password";
$txt = '{
    "fields": {
        "project": {
            "key": "KEY"
        },
        "summary": "SUMMARY",
        "description": "DESCRIPTION",
        "issuetype": {
            "name": "ISSUETYPE"
        }
    }
}';

// Create a new cURL resource
$ch = curl_init ();

// Set URL and other appropriate options
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $txt );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_USERPWD, $username . ":" . $password );

$headers = array ();
$headers [] = "Content-Type: application/json";
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );

// Grab URL and pass it to the browser
$result = curl_exec ( $ch );

echo $result;

答案 2 :(得分:0)

2021 年更新,您可以使用此 PHP Jira REST 客户端:https://github.com/lesstif/php-jira-rest-client

<?php
require 'vendor/autoload.php';

use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;

try {
    $issueField = new IssueField();

    $issueField->setProjectKey("TEST")
                ->setSummary("something's wrong")
                ->setAssigneeName("lesstif")
                ->setPriorityName("Critical")
                ->setIssueType("Bug")
                ->setDescription("Full description for issue")
                ->addVersion(["1.0.1", "1.0.3"])
                ->addComponents(['Component-1', 'Component-2'])
                // set issue security if you need.
                ->setSecurityId(10001 /* security scheme id */)
                ->setDueDate('2019-06-19')
            ;
    
    $issueService = new IssueService();

    $ret = $issueService->create($issueField);
    
    //If success, Returns a link to the created issue.
    var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
    print("Error Occured! " . $e->getMessage());
}

并链接到 Jira docs