[英]How to parse Google Fit Json String with PHP
Am trying to parse a response i get from GoogleFit API.我正在尝试解析我从 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.第 4 行是 Logging 而不是响应。
true
I want to store access_token
, refresh_token
, and expires_in
in my db
.我想在我的
db
存储access_token
、 refresh_token
和expires_in
。 I cant access the properties of the response either.我也无法访问响应的属性。 Please help
请帮忙
You can decode/parse the JSON response in the following ways:您可以通过以下方式解码/解析 JSON 响应:
for the second option, use true
in json_decode()
对于第二个选项,在
json_decode()
使用true
ie 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'];
Some APIs respond with an invalid JSON.某些 API 以无效的 JSON 进行响应。 They add an boolean expression (true or 1) after the JSON object for security reasons.
出于安全原因,他们在 JSON 对象之后添加了一个布尔表达式(true 或 1)。 You might have to prehandle the response by yourself before parsing.
在解析之前,您可能必须自己预先处理响应。
I assume you are encoding the $result for your logging.我假设您正在为您的日志记录 $result 编码。 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.之后,您可以使用
json_decode($newResult, true)
- 它基本上将其转换为数组,您可以获得所需的相关值。
https://www.php.net/manual/en/function.json-decode.php https://www.php.net/manual/en/function.json-decode.php
$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:现在您可以将
$json
变量作为数组使用:
echo $json['access_token'];
echo $json['token_type'];
Thanks, everyone that contributed to this thread.谢谢,所有为此线程做出贡献的人。 However, I found a library online that make things easier for me, Google API PHP Client
但是,我在网上找到了一个让我更轻松的库, Google API PHP Client
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.