1

We are retrieving our user's move minutes from Google Fit using the following standard PHP code and cURL:

$fields = array(
        'startTimeMillis'=> strtotime($start_date) * 1000,
        'endTimeMillis'=> strtotime($end_date) * 1000,
        'aggregateBy' => array(
                'dataSourceId'=> "derived:com.google.activity.segment:com.google.android.gms:merge_activity_segments",
         ),
         'bucketByTime' => array(
                'durationMillis'=> "86400000",
         ),
    );
    $payload = json_encode($fields);
    $payload = '{"startTimeMillis":"' . strtotime($start_date) * 1000 . '","endTimeMillis":"' . strtotime($end_date) * 1000 . '","aggregateBy":[{"dataSourceId":"derived:com.google.activity.segment:com.google.android.gms:merge_activity_segments"}],"bucketByTime":{"durationMillis":86400000}}';
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Authorization: Bearer ' . $new_access_token,
      'dataType: json',
      'Content-Type: application/json; charset=utf-8',
      'Content-Length: ' . strlen($payload)
   ));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);  
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);                  
    curl_setopt($ch,CURLOPT_POSTFIELDS, $payload); 
    $response = curl_exec( $ch );
    $data = json_decode($response, TRUE);
    foreach($data['bucket'] as $key => $value) {
        $var_date = $data["bucket"][$key]['startTimeMillis'];
        foreach($data["bucket"][$key]['dataset'] as $k=>$v){
            foreach($v as $a=>$b){
                if($a == 'point'){
                    foreach($b as $c=>$d){
                        $var_count = 1;
                        foreach($d['value'] as $f=>$g){
                            if($var_count == 2){
                                $duration = $g['intVal'] / 1000;
                                $var_count = 1;
                            } else {
                                $var_count = $var_count + 1;
                            }
                        }
                    }
                }
            }
       }
   }

We receive the correct number of seconds for just about all users except those that are using Apple Watch. We receive values for the watch users, but they are usually over or under the numbers reported inside the user's Google Fit app itself by wide margins and we can't figure out the discrepency.

0 Answers0