How to parse Google Fit Json String with PHP

parse google sheet json
google fit rest api example
google fit api tutorial
php-google spreadsheet-client example
google fit rest api get steps
google fit api get data
google sheet to json
google fit api java

Am trying to parse a response i get from GoogleFit API. Here is the snippet i wrote:

1 $result = curl_exec($ch);//execute post
2 curl_close($ch);//close connection 
3 $newResult = json_encode($result); 
4 Log::info($newResult); 
5 return $newResult;

And the response looks like this:

{ "access_token": "ya29.Il-4B1111", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "1//09uJO5Lo7CFhyCg3333", "scope": "https://www.googleapis.com/auth/fitness.activity.read https://www.googleapis.com/auth/fitness.location.read" } true

Line 4 is Logging instead of the response.

true

I want to store access_token, refresh_token, and expires_in in my db. I cant access the properties of the response either. Please help

You can decode/parse the JSON response in the following ways:

  1. object
  2. PHP associative array

for the second option, use true in json_decode()

i.e. you can use following:

<?php
const NL = PHP_EOL;

$json = '{
    "access_token": "ya29.Il-4B1111",
    "token_type": "Bearer",
    "expires_in": 3600,
    "refresh_token": "1//09uJO5Lo7CFhyCg3333",
    "scope": "https://www.googleapis.com/auth/fitness.activity.read https://www.googleapis.com/auth/fitness.location.read"
}';

// object
$jsonObj = json_decode($json);
echo $jsonObj->access_token;
echo NL;
echo $jsonObj->refresh_token;
echo NL;
echo $jsonObj->expires_in;
echo NL;

// associative array
$jsonArr = json_decode($json, true);
echo $jsonArr['access_token'];
echo NL;
echo $jsonArr['refresh_token'];
echo NL;
echo $jsonArr['expires_in'];

working demo

How to parse Google Fit Json String with PHP, Am trying to parse a response i get from GoogleFit API. Here is the snippet i wrote : 1 $result = curl_exec($ch);//execute post 2� Decoding JSON data using PHP. You can parse or read JSON data from JSON object or any JSON file using PHP json_decode() method. Different examples of parsing JSON data using PHP are given below. Example-1: In the following example, JSON data is assigned in a variable and PHP json_decode() method is used to read the data in PHP format.

Some APIs respond with an invalid JSON. They add an boolean expression (true or 1) after the JSON object for security reasons. You might have to prehandle the response by yourself before parsing.

Performance Tips | Google Fit, This lets your application avoid transferring, parsing, and storing unneeded fields , so it can use resources including network, CPU, and memory� The JSON.parse() function is included in all major browsers and in the latest ECMAScript (JavaScript) standard. The numbers in the table below specifies the first browser version that fully supports the JSON.parse() function:

I assume you are encoding the $result for your logging. After that, you can use json_decode($newResult, true) - which basically converts this into an array and you can get the relevant values you need.

https://www.php.net/manual/en/function.json-decode.php

PHP for parsing the JSON output a published Google spreadsheet , android api android-sensors google-fit activity-recognition 28 views forks PHP A Google Fit API wrapper for viewing and exporting data. parser.js. 8 views� Standard functions are available in PHP for generating, parsing and interpreting JSON format. The PHP program in this how-to guide reads a JSON file into a JSON string and uses json_decode() function to convert the JSON string into a PHP variable. JSON data consists of nested key-value pairs.

$url = 'YOUR API URL GOES HERE';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));

$result = curl_exec($cURL);

curl_close($cURL);   

$json = json_decode($result, true);
print_r($json);

OUTPUT

Array
(
    [access_token] => ya29.Il-4B1111
    [token_type] => Bearer
    //....
)

Now you can work with $json variable as an array:

echo $json['access_token'];
echo $json['token_type'];

Google Fit, In this JSON Parsing in Android Tutorial we will learn Fetching Data from MySQL and Displaying it in ListView 3.4 The Web Service URL; 3.5 Fetching JSON String Now our PHP and MySQL part is over, lets learn JSON Parsing in Android. Now, upload the same APK bundle in Google play store. Parsing JSON with PHP. JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode(), respectively. Both functions only works with UTF-8 encoded string data. Encoding JSON Data in PHP

Thanks, everyone that contributed to this thread. However, I found a library online that make things easier for me, Google API PHP Client

JSON Parsing in Android – Fetching From MySQL Database, The Parse PHP SDK is fully open source, and anyone can contribute to make it better, Note that it is not guaranteed that 'response' will be a parsable json array. If the response cannot be decoded it will be returned as a string instead. sometimes even after you've shipped your app to the App Store or Google Play. Convert the object into a JSON string. Send a request to the PHP file, with the JSON string as a parameter. Wait until the request returns with the result (as JSON) Display the result received from the PHP file. Take a look at the PHP file:

PHP Developers Guide, Let's parse the JSON response from the API that we receive. If Google cannot find three matches for our book query, in that case we wouldn't be receiving three results The problem is that they are using php to get the roll number and return a pdf file.I want to Can a web component be used to go to url and play video? Parsing JSON String Into Java Objects. GSON Api consists a Class named Gson in the package 'com.google.gson' which contains a method named fromJson() with that method we can parse Json String to Java Object.

Web Component – JSON Response On App Inventor, Learn how to import JSON data from API to Google Sheets using user defined IMPORTJSON Duration: 7:30 Posted: 4 Sep 2017 The value of any JSON key can be a string, Boolean, number, null, array, or object. Comments are not allowed in JSON. Although JSON resembles an object or an array, JSON is a string. A serialized string, which means it can later be parsed and decoded into data types. Using data from JSON with PHP

IMPORTJSON Function - Google Sheets Tutorial, It simply fetch data from mysql database table and convert into JSON string format by using Duration: 4:20 Posted: 2 May 2016 Syntax JSON.parse(text[, reviver])Parameters text The string to parse as JSON. See the JSON object for a description of JSON syntax. reviver Optional If a function, this prescribes how the value originally produced by parsing is transformed, before being returned.

Comments
  • curl_exec() returns true or false by default. Have you changed that behaviour?
  • Please what should I change curl_exec() to @ÁlvaroGonzález??
  • This should work but the problem is 1 is appended to the response [As seen here][1] [1]: 3v4l.org/71W7r
  • use e.g. rtrim() demo
  • The reason why you get 1 is your API retrun a redirect response instead of valid JSON data
  • Invalid output for security reasons? Do you have any link to back up that?
  • Yes this what happens are boolean response was appended to the response, but i dont know how to handle this, please help
  • this returns: { "access_token": "ya29.Il-4B0HKn5VqyqTeff7C87JKjNgat5EkHkDLYiuHh1PXrIajQHB2Yitr6L0I5_cTSouqz_UjjqghpES6W0fSpXXds1xSyQedg415aRSpmMfJLQzUZvlcLy6y5MFbRLY6Kg", "token_type": "Bearer", "expires_in": 3599, "refresh_token": "1//0c5cwmuTVt4xBCgYIARAAGAwSNwF-L9Irc_-5zL26cuUikEW08chsnjQfKukp6H8HZ0VpQ9Qd4G7mbKurE4-BYZ4TX7FGP765Ra8", "scope": "https://www.googleapis.com/auth/fitness.activity.read https://www.googleapis.com/auth/fitness.location.read" } 1 and echo $json['access_token']; is empty string
  • Could you please paste your API URL here so i could give it a try ?
  • The link you sent to me return a 302 HTTP response code (a redirect response)
  • try again please