亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PHP 解析 Google Fit Json 字符串

如何使用 PHP 解析 Google Fit Json 字符串

PHP
哈士奇WWW 2022-07-16 16:18:45
我正在嘗試解析我從 GoogleFit API 獲得的響應。這是我寫的片段:1 $result = curl_exec($ch);//execute post2 curl_close($ch);//close connection 3 $newResult = json_encode($result); 4 Log::info($newResult); 5 return $newResult;響應如下所示:{ "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第 4 行是 Logging 而不是響應。true我想將access_token,refresh_token和存儲expires_in在我的db. 我也無法訪問響應的屬性。請幫忙
查看完整描述

4 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

您可以通過以下方式解碼/解析 JSON 響應:


目的

PHP 關聯數組

對于第二個選項,true使用json_decode()


即您可以使用以下內容:


<?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'];


查看完整回答
反對 回復 2022-07-16
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

某些 API 以無效的 JSON 響應。出于安全原因,他們在 JSON 對象之后添加了一個布爾表達式(true 或 1)。在解析之前,您可能必須自己預先處理響應。



查看完整回答
反對 回復 2022-07-16
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

我假設您正在為您的日志記錄編碼 $result。之后,您可以使用json_decode($newResult, true)- 基本上將其轉換為數組,您可以獲得所需的相關值。

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


查看完整回答
反對 回復 2022-07-16
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

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

輸出


Array

(

    [access_token] => ya29.Il-4B1111

    [token_type] => Bearer

    //....

)

現在您可以將$json變量用作數組:


echo $json['access_token'];

echo $json['token_type'];


查看完整回答
反對 回復 2022-07-16
  • 4 回答
  • 0 關注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號