2016-04-11 72 views
3

PHPライブラリ(https://github.com/djchen/oauth2-fitbit)を使用して、Oauth2を介してユーザーのFitbitデータを取得しています。私はデータを正しく取得していますが、多次元配列の応答から特定の項目を取得する方法がわかりません。PHPでのFitbit APIの応答処理

私は以下のコードが、doesntの仕事

$response = $provider->getResponse($request); 
     var_dump($response['encodedId'][0]); 

完全なPHPコード

$provider = new djchen\OAuth2\Client\Provider\Fitbit([ 
     'clientId'   => 'xxx', 
     'clientSecret'  => 'xxx', 
     'redirectUri'  => 'http://xxx-env.us-east-1.elasticbeanstalk.com/a/fitbitapi' 
    ]); 

    // start the session 
    session_start(); 

    // If we don't have an authorization code then get one 
    if (!isset($_GET['code'])) { 

     // Fetch the authorization URL from the provider; this returns the 
     // urlAuthorize option and generates and applies any necessary parameters 
     // (e.g. state). 
     $authorizationUrl = $provider->getAuthorizationUrl(); 

     // Get the state generated for you and store it to the session. 
     $_SESSION['oauth2state'] = $provider->getState(); 

     // Redirect the user to the authorization URL. 
     header('Location: ' . $authorizationUrl); 
     exit; 

    // Check given state against previously stored one to mitigate CSRF attack 
    } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { 
     unset($_SESSION['oauth2state']); 
     exit('Invalid state'); 

    } else { 

     try { 

      // Try to get an access token using the authorization code grant. 
      $accessToken = $provider->getAccessToken('authorization_code', [ 
       'code' => $_GET['code'] 
      ]); 

      // We have an access token, which we may use in authenticated 
      // requests against the service provider's API. 
      echo $accessToken->getToken() . "\n"; 
      echo $accessToken->getRefreshToken() . "\n"; 
      echo $accessToken->getExpires() . "\n"; 
      echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n"; 

      // Using the access token, we may look up details about the 
      // resource owner. 
      $resourceOwner = $provider->getResourceOwner($accessToken); 

      var_export($resourceOwner->toArray()); 

      // The provider provides a way to get an authenticated API request for 
      // the service, using the access token; it returns an object conforming 
      // to Psr\Http\Message\RequestInterface. 
      $request = $provider->getAuthenticatedRequest(
       'GET', 
       'https://api.fitbit.com/1/user/-/profile.json', 
       $accessToken 
      ); 
      // Make the authenticated API request and get the response. 
      $response = $provider->getResponse($request); 
      var_dump($response['encodedId'][0]); 

レスポンスデータ

eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NjAzNzgxOTYsInNjb3BlcyI6InJ3ZWkgcnBybyByaHIgcmxvYyBを使用していますyc2xlIHJzZXQgcmFjdCByc29jIiwic3ViIjoiNEg4NU5WIiwiYXVkIjoiMjI3UUNXIiwiaXNzIjoiRml0Yml0IiwidHlwIjoiYWNjZXNzX3Rva2VuIiwiaWF0IjoxNDYwMzc0NTk2fQ.NN9OOx - 3YLvwai0hl0ZRJ4MNWXlaMwcEJ_xxxxxb2382a930144c3a76e69567dcbf0d9834c574919fff8c268b378e635735f1bbf 1460378196ません期限切れの配列( 'encodedId' => '4545NV'、 'のdisplayName' => 'ダン')...

答えて

0

私はFitBit APIで同じPHPライブラリを使用しています統合。

:あなたはFitBitからユーザープロファイルを取得しようとすると

 // requests against the service provider's API. 
     echo $accessToken->getToken() . "\n"; 
     echo $accessToken->getRefreshToken() . "\n"; 
     echo $accessToken->getExpires() . "\n"; 
     echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n"; 

     // Using the access token, we may look up details about the 
     // resource owner. 
     $resourceOwner = $provider->getResourceOwner($accessToken); 

     var_export($resourceOwner->toArray()); 

は、あなたが以下のリクエストを行います。あなたが質問して貼り付けている応答があるため、コードの以下の部分から来ているデータであり、

 $request = $provider->getAuthenticatedRequest(
      'GET', 
      'https://api.fitbit.com/1/user/-/profile.json', 
      $accessToken 
     ); 
     // Make the authenticated API request and get the response. 
     $response = $provider->getResponse($request); 

$応答は以下の形式で表示され、そこには「encodeId」が直接キーではないことがわかります。以下はvar_dump($ response)の例です。 -

Array(
[user] => Array 
    (
     [age] => 27 
     [avatar] => https://static0.fitbit.com/images/profile/defaultProfile_100_male.gif 
     [avatar150] => https://static0.fitbit.com/images/profile/defaultProfile_150_male.gif 
     [averageDailySteps] => 3165 
     [corporate] => 
     [dateOfBirth] => 1991-04-02 
     [displayName] => Avtar 
     [distanceUnit] => METRIC 
     [encodedId] => 478ZBH 
     [features] => Array 
      (
       [exerciseGoal] => 1 
      ) 

     [foodsLocale] => en_GB 
     [fullName] => Avtar Gaur 
     [gender] => MALE 
     [glucoseUnit] => METRIC 
     [height] => 181 
     [heightUnit] => METRIC 
     [locale] => en_IN 
     [memberSince] => 2016-01-17 
     [offsetFromUTCMillis] => 19800000 
     [startDayOfWeek] => MONDAY 
     [strideLengthRunning] => 94.2 
     [strideLengthRunningType] => default 
     [strideLengthWalking] => 75.1 
     [strideLengthWalkingType] => default 
     [timezone] => Asia/Colombo 
     [topBadges] => Array 
      (
       [0] => Array 
        (
        ) 

       [1] => Array 
        (
        ) 

       [2] => Array 
        (
        ) 

      ) 

     [waterUnit] => METRIC 
     [waterUnitName] => ml 
     [weight] => 80 
     [weightUnit] => METRIC 
    ) 

あなたは、このようにそれにアクセスする必要があり、そこに何かをアクセスするために -

$encodedId = $response['user']['encodedId]; 

私は、これは参考になりました願っています。 fitbit APIに関連するその他の質問については、Fitbit Subscriver APIやNotificationsなどのすべての機能を備えているので、質問することができます。

関連する問題