类stdClass的对象不能转换为字符串PHP问题 [英] Object of class stdClass could not be converted to string PHP issue

查看:9971
本文介绍了类stdClass的对象不能转换为字符串PHP问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可是从吉拉Atlassian的API拉动信息,香港专业教育学院与其他code的工作,它工作正常,我将其更改为JSON和所有的信息被返回的对象。

但是,在这种情况下,我得到这个服务器错误:
PHP开捕致命错误:类stdClass的客体不能转换为字符串/usr/local/vhosts/999/webspace/httpdocs/mysite/filters1.php线411

现在这里是code我尝试运行:

  $用户名='xxx@gmail.com';
$密码='XXXXXXX';$ URL = 'xxx.atlassian.net/rest/api/2/search?jql=project+%3D+bug+AND+component+%3D+%22issues%22+AND+status+in+(Open,+Reopened)';
$头=阵列(
接受:应用/ JSON,
内容类型:应用程序/ JSON
);
$ CH = curl_init();
$测试=这是自定义字段的内容。curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
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,$头);
curl_setopt($ CH,CURLOPT_CUSTOMREQUEST,GET);
// curl_setopt($ CH,CURLOPT_POSTFIELDS,$数据);
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_USERPWD,$用户名:密码$);
$结果= curl_exec($ CH);
$ issue_json = json_de code($结果);
$ CH_ERROR = curl_error($ CH);如果($ CH_ERROR){
    回声卷曲错误:$ CH_ERROR
}其他{
    回声$ issue_json;
}curl_close($ CH);

正如我所说,它返回以上,但错误,如果我改变code到

  $结果= curl_exec($ CH);
 // $ issue_json = json_de code($结果);

然后我回声出$导致它的工作原理,并返回我需要的一切,但作为一个巨大的字符串,而不是一个对象像我想要的。我格式化的下方,它有点更具可读性整个字符串。
我用Google搜索了一些类似的问题,我想我需要做的是这样的:

 的foreach($字符串作为$字符串)
{
     回声$字符串;
}

但我不是太熟悉这种东西只是基本的PHP所以我是一个有点失落

  {扩展:
                的名字,纲目,
                startAt:0,
                的maxResults:50,
                总:1,
                问题:
                        [{扩大:
                        editmeta,
                        renderedFields,
                        过渡,
                        更新日志,
                        操作,
                        ID:15550,
                        自我:https://testing.atlassian.net/rest/api/2/issue/15550
                        钥匙:虫-4990
                        田:{进步:{进步:0,总:0},
                        总结:球员摘要组配置文件柱状图不显示所有范围,
                        问题类型:{自:https://testing.atlassian.net/rest/api/2/issuetype/1,
                        ID:1
                        说明:损害或prevents产品的功能的问题,
                        iconUrl:http://www.google.com/images/bug_16.png,
                        名:错误,
                        子任务:虚假},
                        票:{自我:https://testing.atlassian.net/rest/api/2/issue/bug-4990/votes
                        票:0,
                        hasVoted:虚假},
                        customfield_11100:579,
                        决议:空,
                        fixVersions:
                                [{自我:https://testing.atlassian.net/rest/api/2/version/10301
                                ID:10301,
                                说明:臭虫会在未来正式发布一次,目前的版本中会被压,暂时在这里同时举行的
                                名:里程碑1
                                存档:假的,
                                释放:假}],
                        customfield_10403:{
                                            自我:https://testing.atlassian.net/rest/api/2/customFieldOption/10105
                                            值:低,ID:10105},
                                            resolutiondate:空,
                                            customfield_11101:空,
                                            timespent:空,
                                            记者:{
                                                        自我:https://testing.atlassian.net/rest/api/2/user?username=donalduck%4moo.ca
                                                        名:bugsbunny@gmail.com
                                                        EMAILADDRESS:bugsbunny@gmail.com
                                                        avatarUrls:{
                                                                    16×16:https://testing.atlassian.net/secure/useravatar?size=small&avatarId=10122,
                                                                    48×48:https://testing.atlassian.net/secure/useravatar?avatarId=10122},
                                                                    显示名:兔八哥
                                                                    主动:真正},
                        customfield_10203:空,
                        customfield_10205:[{
                                            自我:https://testing.atlassian.net/rest/api/2/user?username=donaldduck%40gmail.com
                                            名: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\"},
                                                        显示名:唐老鸭,主动:真正},{
                                            自我:https://testing.atlassian.net/rest/api/2/user?username=daffyduck%40holdemmanager.net
                                            名: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\"},
                                                        显示名:达菲鸭,主动:真正},{
                                            自我:https://testing.atlassian.net/rest/api/2/user?username=wazzy%40gmail.com
                                            名: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\"},
                                                        显示名:wazzy,
                                                        主动:真正},
                                            {自我:https://testing.atlassian.net/rest/api/2/user?username=joey%40gmail.com
                                            名:bugsbunny@gmail.com
                                            EMAILADDRESS:bugsbunny@gmail.com
                                            avatarUrls:{
                                                        16×16:https://testing.atlassian.net/secure/useravatar?size=small&avatarId=10122,
                                                        48×48:https://testing.atlassian.net/secure/useravatar?avatarId=10122},
                                                        显示名:兔八哥
                                                        主动:真正},
                                            {自我:https://testing.atlassian.net/rest/api/2/user?username=moocowgmail.com
                                            名: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\"},
                                                        显示名:哞牛,主动:真正}],
                                            更新:2012-09-10T14:25:45.000-0600,创造:2012-05-24T13:47:03.000-0600
                        customfield_11001:空,
                        说明:V158见所附图片的一切是错误的。
                        优先:{
                                            自我:https://testing.atlassian.net/rest/api/2/priority/11
                                            iconUrl:http://dl.dropbox.com/no4.gif,
                                            名:4 - 低优先级,ID:11},
                                            customfield_11000:空,
                                            issuelinks:[],
                                            手表:{
                                                    自我:https://testing.atlassian.net/rest/api/2/issue/bug-4990/watchers
                                                    watchCount:6,
                                                    isWatching:真正},
                        customfield_10600:空,
                        状态:{自我:https://testing.atlassian.net/rest/api/2/status/1
                        说明:这个问题是开放的,并准备受让人开始它的工作
                        iconUrl:https://testing.atlassian.net/images/icons/status_open.gif,
                        名:打开,ID:1},
                        标签:[],
                        workratio: - 1,
                        受让人:{
                                            自我:https://testing.atlassian.net/rest/api/2/user?username=igloo
                                            名:冰屋,
                                            EMAILADDRESS:igloo@yahoo.com
                                            avatarUrls:{
                                                        \"16x16\":\"https://testing.atlassian.net/secure/useravatar?size=small&ownerId=igloo&avatarId=10800\",
                                                        48×48:https://testing.atlassian.net/secure/useravatar?ownerId=igloo&avatarId=10800},
                                                        显示名:冰屋,
                                                        主动:真正},
                        customfield_10202:空,
                        customfield_10200:空,
                        customfield_10501:空,
                        customfield_10500:空,
                        项目:{
                                            自我:https://testing.atlassian.net/rest/api/2/project/HM
                                            ID:10000,
                                            钥匙:错误,
                                            名:窃听去,
                                            avatarUrls:{
                                                        \"16x16\":\"https://testing.atlassian.net/secure/projectavatar?size=small&pid=10000&avatarId=10200\",
                                                        48×48:https://testing.atlassian.net/secure/projectavatar?pid=10000&avatarId=10200}},
                        customfield_10805:空,
                        customfield_10700:空,
                        customfield_10400:空,
                        customfield_11200:空,
                        timeestimate:空,
                        customfield_11201:未启动
                        lastViewed:空,
                        customfield_10800:空,
                        组件:[{
                                            自我:https://testing.atlassian.net/rest/api/2/component/10018,ID:10018,
                                            名:图形},{
                                                            自我:https://testing.atlassian.net/rest/api/2/component/10200,ID:10200,
                                                            名:混合问题,说明:用图表问题},{
                                                                    自我:https://testing.atlassian.net/rest/api/2/component/10011,ID:10011,
                                                                    名:Curoers}],
                        customfield_10803:空,
                        timeoriginalestimate:空,
                        customfield_10804:空,
                        customfield_10801:空,
                        customfield_10900:{自:https://testing.atlassian.net/rest/api/2/customFieldOption/10201,
                        值:否,
                        ID:10201},
                        customfield_10802:空}}]}


解决方案

功能 json_de code 不返回一个字符串。

看一看该函数的文档。

<一个href=\"http://fr2.php.net/function.json-de$c$c.php\">http://fr2.php.net/function.json-de$c$c.php

问题是,你想呼应的对象。下面code出现故障。

 回声$ issue_json;

您可以用var_dump看到来自json_de code返回的对象的结构。

 的var_dump($ issue_json);

Im pulling info from Jira Atlassian API, ive worked with other code and it works fine and i change it to json and all the info gets returned as an object.

However in this case i get this server error: PHP Catchable fatal error: Object of class stdClass could not be converted to string in /usr/local/vhosts/999/webspace/httpdocs/mysite/filters1.php on line 411

Now here is the code im trying to run:

$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);

As i mentioned that returns the error above but if i alter the code to

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

and then i echo out $result it works and returns everything i need but as a huge string and not an object like i want. I formatted the whole string below so its a bit more readable. I googled some similar issues and i think i need to do something like this:

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

But i not too familiar with this kind of stuff just basic php so im a bit lost

{"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}}]}

解决方案

The function json_decode does not return a string.

Have a look at the documentation for the function.

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

The problem is that you're trying to echo the object. The following code is faulty.

echo $issue_json;

You can use var_dump to see the structure of the object returned from json_decode.

var_dump($issue_json);

这篇关于类stdClass的对象不能转换为字符串PHP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆