我使用一个php库(https://github.com/djchen/oauth2-fitbit)通过oauth2检索用户fitbit数据。我得到的数据是正确的,但我不确定如何从多维数组响应中获取特定项。
我正在使用下面的代码,但不起作用

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

响应数据
在JJJ9.EYJlEJlEJlEJlEEJlEJlEJlEJJlEJJlEJlEJlEJlEJlEJlEJcJcJcJcJcJcJcJcJcJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlWlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlEJlWlW2A930144C3A76E69567DCBF0D9834C574919FFF8C268B378E635735F1BBF 1460378196不过期数组('encodedI'=>'4545nV','displayname'
=>“丹”,)…

最佳答案

我使用相同的php库进行fitbit api集成。您在问题中粘贴的响应是由于代码的以下部分而产生的数据:

     // 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());

当您试图从fitbit获取用户配置文件时,您会发出以下请求:
        $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);

$response的格式如下,您可以看到“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订阅api和通知。

关于php - PHP中的Fitbit API响应处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36547746/

10-11 04:01