how to get email and display name from oneall API?

时间:2016-04-07 10:55:31

标签: php json

{
    "response": {
        "request": {
            "date": "Tue, 13 Sep 2011 11:24:28 +0200",
            "resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
            "status": {
                "flag": "success",
                "code": 200
            }
        },
        "result": {
            "data": {
                "user": {
                    "user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
                    "date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
                    "date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
                    "num_logins": "64",
                    "identities": [{
                        "identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
                        "provider": "twitter",
                        "id": "http://twitter.com/ExampleUser",
                        "displayName": "Firstname Lastname",
                        "name": {
                            "formatted": "Firstname Lastname"
                        },
                        "gender": "male",
                        "utcOffset": "2:00"
                    }, {
                        "identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
                        "provider": "facebook",
                        "id": "http://www.facebook.com/profile.php?id=1046121518",
                        "displayName": "Firstname Lastname",
                        "name": {
                            "formatted": "Firstname Lastname",
                            "givenName": "Firstname",
                            "familyName": "Lastname"
                        },
                        "gender": "male",
                        "birthday": "01/01/1980",
                        "utcOffset": "2:00",
                        "emails": [{
                            "value": "email@example.com",
                            "is_verified": "true"
                        }]
                    }]
                }
            }
        }
    }
}

The above code is Result: the code returned by the ONEALL API I am using oneall api for my website social login ,after i login i requested user details by get method using user token, the values like last login got by using $data->user->date_last_login, but i am unable to get email and dispalyname how can i do that one , i didn't wrote all the code i gave just sample, hope this will help for others also

http://docs.oneall.com/api/resources/users/read-user-details/

1 个答案:

答案 0 :(得分:1)

如果您遵循对象结构,则可以将它们设为:

$data = json_decode($json);
$displayName = $data->response->result->data->user->identities[1]->displayName;
$email = $data->response->result->data->user->identities[1]->emails[0]->value;

更新

如果您使用中间var作为响应数据,那么只需收缩链:

$json = json_decode($result_json);
//extract response data
$data = $json->response->result->data;
$displayName = $data->user->identities[1]->displayName;
$email = $data->user->identities[1]->emails[0]->value;

这会给你:

Firstname Lastname
email@example.com

在这里,我们根据您的JSON对象提供完整的代码:

<?php
    $result_json = '
        {
            "response": {
                    "request": {
                            "date": "Tue, 13 Sep 2011 11:24:28 +0200",
                            "resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
                            "status": {
                                    "flag": "success",
                                    "code": 200
                            }
                    },
                    "result": {
                            "data": {
                                    "user": {
                                            "user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
                                            "date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
                                            "date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
                                            "num_logins": "64",
                                            "identities": [{
                                                    "identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
                                                    "provider": "twitter",
                                                    "id": "http://twitter.com/ExampleUser",
                                                    "displayName": "Firstname Lastname",
                                                    "name": {
                                                            "formatted": "Firstname Lastname"
                                                    },
                                                    "gender": "male",
                                                    "utcOffset": "2:00"
                                            }, {
                                                    "identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
                                                    "provider": "facebook",
                                                    "id": "http://www.facebook.com/profile.php?id=1046121518",
                                                    "displayName": "Firstname Lastname",
                                                    "name": {
                                                            "formatted": "Firstname Lastname",
                                                            "givenName": "Firstname",
                                                            "familyName": "Lastname"
                                                    },
                                                    "gender": "male",
                                                    "birthday": "01/01/1980",
                                                    "utcOffset": "2:00",
                                                    "emails": [{
                                                            "value": "email@example.com",
                                                            "is_verified": "true"
                                                    }]
                                            }]
                                    }
                            }
                    }
            }
        }';

    $json = json_decode($result_json);
    $data = $json->response->result->data;
    echo $displayName = $data->user->identities[1]->displayName;
    echo '<br>';
    echo $email = $data->user->identities[1]->emails[0]->value;
?>