如何从对象获取价值(php)

时间:2016-09-16 11:40:01

标签: php

我在以下对象中有一个变量:

echo "<pre>";
print_r($request);
echo "</pre>";
Facebook\FacebookResponse Object
(
    [httpStatusCode:protected] => 200
    [headers:protected] => Array
        (
            [Access-Control-Allow-Origin] => *
            [ETag] => "xxxxxxx"
            [Pragma] => no-cache
            [Cache-Control] => private, no-cache, no-store, must-revalidate
            [facebook-api-version] => v2.7
            [Expires] => Sat, 01 Jan 2000 00:00:00 GMT
            [Content-Type] => application/json; charset=UTF-8
            [x-fb-trace-id] => xxxxxxx
            [x-fb-rev] => 2568729
            [Vary] => Accept-Encoding
            [X-FB-Debug] => xxxxxxx
            [Date] => Fri, 16 Sep 2016 11:23:14 GMT
            [Connection] => keep-alive
            [Content-Length] => 1006
        )

    [body:protected] => {"data":[{"access_token":"xxxxxxx","category":"Restaurant\/Cafe","category_list":[{"id":"171466272901863","name":"Late Night Restaurant"}],"name":"Karczma janka","id":"199296333814893","perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]},{"access_token":"xxxxxxx","category":"Restaurant\/Cafe","category_list":[{"id":"144722595590046","name":"Greek Restaurant"}],"name":"Jad\u0142odajnia","id":"1055846191130232","perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]}],"paging":{"cursors":{"before":"xxxxxxx","after":"xxxxxxx"}}}
    [decodedBody:protected] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [access_token] => xxxxxxx
                            [category] => Restaurant/Cafe
                            [category_list] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 171466272901863
                                            [name] => Late Night Restaurant
                                        )

                                )

                            [name] => Karczma janka
                            [id] => 199296333814893
                            [perms] => Array
                                (
                                    [0] => ADMINISTER
                                    [1] => EDIT_PROFILE
                                    [2] => CREATE_CONTENT
                                    [3] => MODERATE_CONTENT
                                    [4] => CREATE_ADS
                                    [5] => BASIC_ADMIN
                                )

                        )

                    [1] => Array
                        (
                            [access_token] => xxxxxxx
                            [category] => Restaurant/Cafe
                            [category_list] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 144722595590046
                                            [name] => Greek Restaurant
                                        )

                                )

                            [name] => Jadłodajnia
                            [id] => 1055846191130232
                            [perms] => Array
                                (
                                    [0] => ADMINISTER
                                    [1] => EDIT_PROFILE
                                    [2] => CREATE_CONTENT
                                    [3] => MODERATE_CONTENT
                                    [4] => CREATE_ADS
                                    [5] => BASIC_ADMIN
                                )

                        )

                )

            [paging] => Array
                (
                    [cursors] => Array
                        (
                            [before] => MTk5Mjk2MzMzODE0ODkz
                            [after] => MTA1NTg0NjE5MTEzMDIzMgZDZD
                        )

                )

        )

    [request:protected] => Facebook\FacebookRequest Object
        (
            [app:protected] => Facebook\FacebookApp Object
                (
                    [id:protected] => 1304598089572869
                    [secret:protected] => a32f677958e282068a8c74ffe7ff2bbc
                )

            [accessToken:protected] => xxxxxxx
            [method:protected] => GET
            [endpoint:protected] => /me/accounts
            [headers:protected] => Array
                (
                    [Content-Type] => application/x-www-form-urlencoded
                )

            [params:protected] => Array
                (
                )

            [files:protected] => Array
                (
                )

            [eTag:protected] => 
            [graphVersion:protected] => v2.5
        )

    [thrownException:protected] => 
)

我需要获取并保存到数组或仅字符串:

[body:protected] => {"data":[{"access_token":"xxxxxxx","category":"Restaurant\/Cafe","category_list":[{"id":"171466272901863","name":"Late Night Restaurant"}],"name":"Karczma janka","id":"199296333814893","perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]},{"access_token":"xxxxxxx","category":"Restaurant\/Cafe","category_list":[{"id":"144722595590046","name":"Greek Restaurant"}],"name":"Jad\u0142odajnia","id":"1055846191130232","perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]}],"paging":{"cursors":{"before":"xxxxxxx","after":"xxxxxxx"}}}

我会尝试在两天内完成,但我不会出来:(我是先生。

我可以请你写一个代码来提取更高的价值吗?

非常感谢:) 现在我有了这个:

$wartosc = '{"data":[{"access_token":"EAASihmm0IgUBAGLdqRwBxOq6LoywTG0N4ZChxRoSHiv3I3a7glTFuKurABcOGik9FKTQtqU4rDa0dkwZAG7ZAnCpXIkrTmmfHDLG03VlvEohJWTyQmfbQgbCtZCDUTa28yjkGs3FCvCiNZCekoZCeG35GCPWVp8jPmpBFUdvYzpAZDZD","category":"Restaurant\/Cafe","category_list":[{"id":"171466272901863","name":"Late Night Restaurant"}],"name":"Karczma janka","id":"199296333814893","perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]},{"access_token":"EAASihmm0IgUBAEmRuj3JsgcspHqXxgOtSEXr0RSksoish6WRKZBNZBANidwMhrZCmEQYl1gRC6IcL28pZBgeAKNCJSREijZBwGxuwf8lPGkXV2kRHgspgQaifdkPeaQRCjpsrO5M3d1yCzh5ZAkcN7M9Q1xBO9vrCJBMxQ8J1ycEWhND9oNDzb","category":"Restaurant\/Cafe","category_list":[{"id":"144722595590046","name":"Greek Restaurant"}],"name":"Jad\u0142odajnia","id":"1055846191130232","perms":["ADMINISTER","EDIT_PROFILE","CREATE_CONTENT","MODERATE_CONTENT","CREATE_ADS","BASIC_ADMIN"]}],"paging":{"cursors":{"before":"MTk5Mjk2MzMzODE0ODkz","after":"MTA1NTg0NjE5MTEzMDIzMgZDZD"}}}';

$wartosc = json_decode($wartosc, true);

echo "<pre>";
print_r($wartosc);
echo "</pre>";


Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [access_token] => EAASihmm0IgUBAGLdqRwBxOq6LoywTG0N4ZChxRoSHiv3I3a7glTFuKurABcOGik9FKTQtqU4rDa0dkwZAG7ZAnCpXIkrTmmfHDLG03VlvEohJWTyQmfbQgbCtZCDUTa28yjkGs3FCvCiNZCekoZCeG35GCPWVp8jPmpBFUdvYzpAZDZD
                    [category] => Restaurant/Cafe
                    [category_list] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 171466272901863
                                    [name] => Late Night Restaurant
                                )

                        )

                    [name] => Karczma janka
                    [id] => 199296333814893
                    [perms] => Array
                        (
                            [0] => ADMINISTER
                            [1] => EDIT_PROFILE
                            [2] => CREATE_CONTENT
                            [3] => MODERATE_CONTENT
                            [4] => CREATE_ADS
                            [5] => BASIC_ADMIN
                        )

                )

            [1] => Array
                (
                    [access_token] => EAASihmm0IgUBAEmRuj3JsgcspHqXxgOtSEXr0RSksoish6WRKZBNZBANidwMhrZCmEQYl1gRC6IcL28pZBgeAKNCJSREijZBwGxuwf8lPGkXV2kRHgspgQaifdkPeaQRCjpsrO5M3d1yCzh5ZAkcN7M9Q1xBO9vrCJBMxQ8J1ycEWhND9oNDzb
                    [category] => Restaurant/Cafe
                    [category_list] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 144722595590046
                                    [name] => Greek Restaurant
                                )

                        )

                    [name] => Jadłodajnia
                    [id] => 1055846191130232
                    [perms] => Array
                        (
                            [0] => ADMINISTER
                            [1] => EDIT_PROFILE
                            [2] => CREATE_CONTENT
                            [3] => MODERATE_CONTENT
                            [4] => CREATE_ADS
                            [5] => BASIC_ADMIN
                        )

                )

        )

    [paging] => Array
        (
            [cursors] => Array
                (
                    [before] => MTk5Mjk2MzMzODE0ODkz
                    [after] => MTA1NTg0NjE5MTEzMDIzMgZDZD
                )

        )

)

我需要使用此数组的foreach(?):

- access_token (eg. EAASihmm0IgUBAOVlH3gCnNvZBADYjAsgUKjGTzRHDdGyEpeTTEPCnZBP6ROFZAHp3MZCScXnmQVsfmCgpiLSKqUV1KLuIFZBBFtLTyJnEAyo36DeinzAeWOl65TDRuBP5sMdLhZCRBuXcVYTVNYHrqdZCX3lx5oKN8h4N2ZC0yHKOwZDZD),
- category (eg. Restaurant/Cafe)
- id1 (eg. 171466272901863)
- name (eg. Karczma janka),
- id2 (eg.  199296333814893)

在我的例子中,我有2家餐厅:Karczma janka和Jadłodajnia。 最后可能有任何数字(最多100个)。

我怎样才能获得这些价值?

1 个答案:

答案 0 :(得分:1)

我可以看到您正在输出FacebookResponse对象 所以我猜你正在写一些使用Facebook SDK的东西。

让我获取SDK的文档: https://developers.facebook.com/docs/php/FacebookResponse/5.0.0

在这些文档中,我可以看到方法getBody(),它将返回响应的主体。