php - 类 stdClass 的对象无法转换为字符串 PHP 问题

标签 php object jira jira-rest-api

我从 Jira Atlassian API 中提取信息,我使用过其他代码并且工作正常,我将其更改为 json,所有信息都作为对象返回。

但是在这种情况下我得到这个服务器错误: PHP Catchable fatal error: 类 stdClass 的对象无法转换为字符串 in/usr/local/vhosts/999/webspace/httpdocs/mysite/filters1.php 第 411 行

现在这是我要运行的代码:

$username = 'xxx@gmail.com';
$password = 'xxxxxxx';

$url = 'xxx.atlassian.net/rest/api/2/search?jql=project+%3D+bug+AND+component+%3D+%22issues%22+AND+status+in+(Open,+Reopened)';


$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);


$ch = curl_init();
$test = "This is the content of the custom field.";

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");


$result = curl_exec($ch);
$issue_json = json_decode($result);
$ch_error = curl_error($ch);

if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    echo $issue_json;
}

curl_close($ch);

正如我提到的那样返回上面的错误但是如果我将代码更改为

 $result = curl_exec($ch);
 //$issue_json = json_decode($result);

然后我回显 $result 它工作并返回我需要的一切,但作为一个巨大的字符串而不是我想要的对象。我对下面的整个字符串进行了格式化,使其更具可读性。 我用谷歌搜索了一些类似的问题,我想我需要做这样的事情:

foreach($string as $strings) 
{
     echo $strings;
}

但我对这种东西不太熟悉,只是基本的 php,所以我有点迷茫

{"expand":
                "names,schema",
                "startAt":0,
                "maxResults":50,
                "total":1,
                "issues":
                        [{"expand":
                        "editmeta,
                        renderedFields,
                        transitions,
                        changelog,
                        operations",
                        "id":"15550",
                        "self":"https://testing.atlassian.net/rest/api/2/issue/15550",
                        "key":"bug-4990",
                        "fields":{"progress":{"progress":0,"total":0},
                        "summary":"Player Summary group profile bar chart does not show all ranges",
                        "issuetype":{"self":"https://testing.atlassian.net/rest/api/2/issuetype/1",
                        "id":"1",
                        "description":"A problem which impairs or prevents the functions of the product.",
                        "iconUrl":"http://www.google.com/images/bug_16.png",
                        "name":"Bug",
                        "subtask":false},
                        "votes":{"self":"https://testing.atlassian.net/rest/api/2/issue/bug-4990/votes",
                        "votes":0,
                        "hasVoted":false},
                        "customfield_11100":"579",
                        "resolution":null,
                        "fixVersions":
                                [{"self":"https://testing.atlassian.net/rest/api/2/version/10301",
                                "id":"10301",
                                "description":"Bugs that will be going in the next official release once the current release is pushed, held temporarily here in the meantime",
                                "name":"Milestone 1",
                                "archived":false,
                                "released":false}],
                        "customfield_10403":{
                                            "self":"https://testing.atlassian.net/rest/api/2/customFieldOption/10105",
                                            "value":"Low","id":"10105"},
                                            "resolutiondate":null,
                                            "customfield_11101":null,
                                            "timespent":null,
                                            "reporter":{
                                                        "self":"https://testing.atlassian.net/rest/api/2/user?username=donalduck%4moo.ca",
                                                        "name":"bugsbunny@gmail.com",
                                                        "emailAddress":"bugsbunny@gmail.com",
                                                        "avatarUrls":{
                                                                    "16x16":"https://testing.atlassian.net/secure/useravatar?size=small&avatarId=10122",
                                                                    "48x48":"https://testing.atlassian.net/secure/useravatar?avatarId=10122"},
                                                                    "displayName":"Bugs Bunny",
                                                                    "active":true},
                        "customfield_10203":null,
                        "customfield_10205":[{
                                            "self":"https://testing.atlassian.net/rest/api/2/user?username=donaldduck%40gmail.com",
                                            "name":"donaldDuck@gmail.com",
                                            "emailAddress":"donaldDuck@gmail.com",
                                            "avatarUrls":{
                                                        "16x16":"https://testing.atlassian.net/secure/useravatar?size=small&ownerId=donaldduck%40gmail.com&avatarId=10202",
                                                        "48x48":"https://testing.atlassian.net/secure/useravatar?ownerId=donaldduckgmail.com&avatarId=10202"},
                                                        "displayName":"Donald Duck","active":true},{
                                            "self":"https://testing.atlassian.net/rest/api/2/user?username=daffyduck%40holdemmanager.net",
                                            "name":"DaffyDuck@hotmail.com",
                                            "emailAddress":"DaffyDuck@hotmail.com",
                                            "avatarUrls":{
                                                        "16x16":"https://testing.atlassian.net/secure/useravatar?size=small&ownerId=daffduck%4testing.net&avatarId=10809",
                                                        "48x48":"https://testing.atlassian.net/secure/useravatar?ownerId=daffyduck%40testing.net&avatarId=10809"},
                                                        "displayName":"Daffy Duck","active":true},{
                                            "self":"https://testing.atlassian.net/rest/api/2/user?username=wazzy%40gmail.com",
                                            "name":"wazzy@gmail.com",
                                            "emailAddress":"wazzy@gmail.com",
                                            "avatarUrls":{
                                                        "16x16":"https://testing.atlassian.net/secure/useravatar?size=small&ownerId=wazzy%40gmail.com&avatarId=10204",
                                                        "48x48":"https://testing.atlassian.net/secure/useravatar?ownerId=wazzy%40gmail.com&avatarId=10204"},
                                                        "displayName":"wazzy",
                                                        "active":true},
                                            {"self":"https://testing.atlassian.net/rest/api/2/user?username=joey%40gmail.com",
                                            "name":"bugsbunny@gmail.com",
                                            "emailAddress":"bugsbunny@gmail.com",
                                            "avatarUrls":{
                                                        "16x16":"https://testing.atlassian.net/secure/useravatar?size=small&avatarId=10122",
                                                        "48x48":"https://testing.atlassian.net/secure/useravatar?avatarId=10122"},
                                                        "displayName":"Bugs Bunny",
                                                        "active":true},
                                            {"self":"https://testing.atlassian.net/rest/api/2/user?username=moocowgmail.com",
                                            "name":"moocow@gmail.com",
                                            "emailAddress":"moocow@gmail.com",
                                            "avatarUrls":{
                                                        "16x16":"https://testing.atlassian.net/secure/useravatar?size=small&ownerId=moocow40gmail.com&avatarId=10808",
                                                        "48x48":"https://testing.atlassian.net/secure/useravatar?ownerId=moocow%40gmail.com&avatarId=10808"},
                                                        "displayName":"Moo Cow","active":true}],
                                            "updated":"2012-09-10T14:25:45.000-0600","created":"2012-05-24T13:47:03.000-0600",
                        "customfield_11001":null,
                        "description":"v158 See attached pic.  everything is wrong.",
                        "priority":{
                                            "self":"https://testing.atlassian.net/rest/api/2/priority/11",
                                            "iconUrl":"http://dl.dropbox.com/no4.gif",
                                            "name":"4 - Low Priority","id":"11"},
                                            "customfield_11000":null,
                                            "issuelinks":[],
                                            "watches":{
                                                    "self":"https://testing.atlassian.net/rest/api/2/issue/bug-4990/watchers",
                                                    "watchCount":6,
                                                    "isWatching":true},
                        "customfield_10600":null,
                        "status":{"self":"https://testing.atlassian.net/rest/api/2/status/1",
                        "description":"The issue is open and ready for the assignee to start work on it.",
                        "iconUrl":"https://testing.atlassian.net/images/icons/status_open.gif",
                        "name":"Open","id":"1"},
                        "labels":[],
                        "workratio":-1,
                        "assignee":{
                                            "self":"https://testing.atlassian.net/rest/api/2/user?username=igloo",
                                            "name":"igloo",
                                            "emailAddress":"igloo@yahoo.com",
                                            "avatarUrls":{
                                                        "16x16":"https://testing.atlassian.net/secure/useravatar?size=small&ownerId=igloo&avatarId=10800",
                                                        "48x48":"https://testing.atlassian.net/secure/useravatar?ownerId=igloo&avatarId=10800"},
                                                        "displayName":"Igloo",
                                                        "active":true},
                        "customfield_10202":null,
                        "customfield_10200":null,
                        "customfield_10501":null,
                        "customfield_10500":null,
                        "project":{
                                            "self":"https://testing.atlassian.net/rest/api/2/project/HM",
                                            "id":"10000",
                                            "key":"bug",
                                            "name":"Bugging out",
                                            "avatarUrls":{
                                                        "16x16":"https://testing.atlassian.net/secure/projectavatar?size=small&pid=10000&avatarId=10200",
                                                        "48x48":"https://testing.atlassian.net/secure/projectavatar?pid=10000&avatarId=10200"}},
                        "customfield_10805":null,
                        "customfield_10700":null,
                        "customfield_10400":null,
                        "customfield_11200":null,
                        "timeestimate":null,
                        "customfield_11201":"Not Started",
                        "lastViewed":null,
                        "customfield_10800":null,
                        "components":[{
                                            "self":"https://testing.atlassian.net/rest/api/2/component/10018","id":"10018",
                                            "name":"Graphs"},{
                                                            "self":"https://testing.atlassian.net/rest/api/2/component/10200","id":"10200",
                                                            "name":"Hybrid Issues","description":"Issues with graphs"},{
                                                                    "self":"https://testing.atlassian.net/rest/api/2/component/10011","id":"10011",
                                                                    "name":"Curoers"}],
                        "customfield_10803":null,
                        "timeoriginalestimate":null,
                        "customfield_10804":null,
                        "customfield_10801":null,
                        "customfield_10900":{"self":"https://testing.atlassian.net/rest/api/2/customFieldOption/10201",
                        "value":"No",
                        "id":"10201"},
                        "customfield_10802":null}}]}

最佳答案

json_decode 函数不返回字符串。

查看该函数的文档。

http://fr2.php.net/function.json-decode.php

问题是您正在尝试回显该对象。以下代码是错误的。

echo $issue_json;

您可以使用 var_dump 查看从 json_decode 返回的对象的结构。

var_dump($issue_json);

关于php - 类 stdClass 的对象无法转换为字符串 PHP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13735592/

相关文章:

php - Mysql 查询显示两列之间的结果

c++ - 使用 "this"的成员函数的返回值

curl - 使用 REST API 更新 JIRA 票证状态

calendar - JIRA 允许检查昨天的任务吗?

plugins - 我可以将 JIRA 权限方案分配给插件中的项目吗?

PHP 每页显示 n 个帖子

php - Laravel 5.2 在同一页面上更新 Eloquent Master Detail

javascript - 如何在 JavaScript 中将对象值转换为新数组

java - 有效的对象映射

javascript - 如何使用 (PHP)foreach 使用数据库中的记录更新我的 javascript 时间表?