在PHP中格式化Jira Atlassian问题url中的长结构化文本字符串

时间:2012-11-22 03:09:19

标签: php object jira

我想知道是否有一种简单的方法来访问我所拥有的巨大字符串中的信息,字符串是结构化的,为了人们审阅它我放置换行符和空格但这只是一个巨大的单行文本那是归还的:

首先,这是我访问Jira API的方式:

$username = 'xxx';
$password = 'xxx';

$url = 'https://xxx.atlassian.net/rest/api/2/Issue/Bug-5555';

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$issue_list = (curl_exec($curl));
echo $issue_list;

现在返回一个巨大的字符串,当它被分解时如下所示:

{"expand":
"renderedFields,names,schema,transitions,operations,editmeta,changelog",
"id":"16935",
"self":"https://xxx.atlassian.net/rest/api/2/issue/16935",
"key":"xx-5555",
"fields":
        {"summary":"Dialog boxes shouldn't be on top.",
        "progress":
                    {"progress":0,
                    "total":0
                    },
        "timetracking":{},
        "issuetype":
                        {"self":"https://xxx.atlassian.net/rest/api/2/issuetype/1",
                    "id":"1",
                    "description":"A problem which impairs or prevents the functions of the product.",
                    "iconUrl":"https://xxx.atlassian.net/images/icons/bug.gif",
                    "name":"Bug",
                    "subtask":false
                    },
        "timespent":null,
        "reporter":
                    {"self":"https://xxx.atlassian.net/rest/api/2/user?username=xxx%40xxx.com",
                    "name":"xxx@xx.com",
                    "emailAddress":"xxx@xxx.com",
                    "avatarUrls":{"16x16":"https://xxx.atlassian.net/secure/useravatar?size=small&avatarId=10122",
                    "48x48":"https://xxx.atlassian.net/secure/useravatar?avatarId=10122"},
                    "displayName":"xxx",
                    "active":true
                    },
        "created":"2012-08-25T18:39:27.760-0600",
        "updated":"2012-08-31T16:47:38.761-0600",
        "priority":
                    {"self":"https://xxx.atlassian.net/rest/api/2/priority/6",
                    "iconUrl":"http://dl.dropbox.com/u/xxx/3-green.png",
                    "name":"3 - Medium Priority",
                    "id":"6"
                    },
        "description":"\"loading \" dialog is always on top, so is the \"Updating database\" dialog.\r\n\r\n\r\nThis is annoying. It shouldn't be on top and/or you should be able to easily minimize the window.",
        "issuelinks":[], etc etc etc

现在我是一个基本的php用户,所以请尽可能简单地保持回复简单,然后我走下解析整个文档的路线,这对我来说很难,因为我不熟悉解析我想知道是否有一个轻松访问值的方法。

我的想法是这样的:

foreach($issue_list->issues as $issue) {
    echo "summary" . $issue->summary;
    echo "updated" . $issue->updated;
    echo "created" . $issue->created;
    echo "description" . $issue->description;
}

现在这可能是一厢情愿的想法,但我看到一篇文章,其中我做了类似的事情,但我无法弄明白,这里是文章: http://www.lornajane.net/posts/2012/using-jiras-rest-api-to-create-a-dashboard

如果有可能,我将如何访问记者> displayName值,因为那是2个缩进深度,它是$ issue-> reporter-> displayName;

最后一个快速的问题,如果我回应描述,我如何让它服从/ r / r / r / r / r / n和/“所以它用换行符打印出去并删除那些特殊的字符?

1 个答案:

答案 0 :(得分:1)

这看起来像一个JSON(JavaScript Object Notation - more info here)字符串,您可以使用json_decode(记录为here)将其转换为PHP对象,然后轻松将其编入索引

我没有完整的字符串,但您可以尝试以下方式:

$jiraIssue = json_decode($theString);
echo $jiraIssue["id"];

现在,由于对象包含在对象内,因此您可能必须先访问"fields"才能访问"summary"

如果您希望处理true而不是array,则可以将object作为第二个参数传递。