0

我想知道是否有一种简单的方法可以访问我拥有的巨大字符串中的信息,该字符串是结构化的,为了人们查看它,我放置了换行符和空格,但这只是返回的一个巨大的单行文本:

首先这是我访问 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 -创建仪表板

此外,如果可能的话,我将如何访问记者 > displayName 值,因为这是 2 个缩进深度,它会是 $issue->reporter->displayName;

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

4

1 回答 1

1

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

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

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

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

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

于 2012-11-22T03:23:55.813 回答