2016-04-07 75 views
0
{ 
    "response": { 
     "request": { 
      "date": "Tue, 13 Sep 2011 11:24:28 +0200", 
      "resource": "https://stackoverflow.com/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 protected]", 
          "is_verified": "true" 
         }] 
        }] 
       } 
      } 
     } 
    } 
} 

上面的代碼是結果:由ONEALL API 我使用oneall API爲我的網站社會登錄返回的代碼後,我登錄我使用用戶令牌的用戶信息通過GET方法,像上次登錄的數值了通過使用$data->user->date_last_login,但我無法得到電子郵件和dispalyname我怎麼做一個,我沒有寫的所有代碼我給剛剛品嚐,希望這將幫助其他人也如何從oneall API獲取電子郵件和顯示名稱?

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

回答

0

如果你按照對象結構,你可以得到它們:

$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 protected] 

在這裏,我們去完成代碼,基於您的JSON對象:

<?php 
    $result_json = ' 
     { 
      "response": { 
        "request": { 
          "date": "Tue, 13 Sep 2011 11:24:28 +0200", 
          "resource": "https://stackoverflow.com/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 protected]", 
                  "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; 
?> 
+0

我沒有得到這些值問題在哪裏,這是我如何創建連接到oneall api $ json = json_decode($ result_json); //提取數據 $ data = $ json-> response-> result-> data; –

+0

罰款,然後只是使鏈較短,看到更新。 – mitkosoft

+0

我用像$ data-> user-> identity [1] - > emails [0] - > value;對於電子郵件,但它沒有得到 –

相關問題