PHP code example of craymend / dexcom-php-sdk

1. Go to this page and download the library: Download craymend/dexcom-php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

craymend / dexcom-php-sdk example snippets




raymend\Dexcom\Request;

$sandboxMode = Request::MODE_SANDBOX;
// $mode = Request::MODE_PRODUCTION;
$redirectUri = 'https://<YOUR DOMAIN>/path/to/receive/oauth/code';
$clientId = 'your-client-id';
$clientSecret = 'your-client-secret';

// "mode" controls the baseUrl
echo 'baseUrl: ' . $request->getBaseUrl();
$request->setMode($sandboxMode);
echo 'baseUrl: ' . $request->getBaseUrl();



// Complete OAuth process
$request = new Request();
$request->setMode($sandboxMode);

// get auth url for user to use OAuth
echo 'OAuth url: ';
echo $request->getAuthUrl($redirectUri, $clientId);

// Get "code" from above auth url.
// Dexcom will return the code to your redirectUri
//   after the user logs in and agrees to give access.
$code = 'code-returned-to-redirect-uri';

// exchange code for refresh and access tokens
$response = $request->exchangeCode($code, $redirectUri, $clientId, $clientSecret);

if (!$response->getStatus()) {
    $errors = $response->getErrors();
    echo json_encode($errors);
} else{
    $data = $response->getData();
    $accessToken = $data['access_token'];
    $refreshToken = $data['refresh_token'];
}

echo "access_token: $accessToken<br>";
echo "refresh_token: $refreshToken<br>";



// Exchange refresh token
$request = new Request();
$request->setMode($sandboxMode);

$response = $request->exchangeRefreshToken($refreshToken, $redirectUri, $clientId, $clientSecret);

if (!$response->getStatus()) {
    $errors = $response->getErrors();
    echo json_encode($errors);
} else{
    $data = $response->getData();
    $accessToken = $data['access_token'];
    $refreshToken = $data['refresh_token'];
}

echo "access_token: $accessToken<br>";
echo "refresh_token: $refreshToken<br>";



$request = new Request($accessToken, $sandboxMode);

$uri = '/users/self/calibrations';
$data = [
    'startDate' => date('Y-m-d\TH:i:s', strtotime('-29 day')),
    'endDate' => date('Y-m-d\TH:i:s')
];
$response = $request->get($uri, $data);
$data = $response->getData();

echo 'data: ' . json_encode($data);

composer