第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

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

4 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以通過以下方式解碼/解析 JSON 響應(yīng):


目的

PHP 關(guān)聯(lián)數(shù)組

對(duì)于第二個(gè)選項(xiàng),true使用json_decode()


即您可以使用以下內(nèi)容:


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


查看完整回答
反對(duì) 回復(fù) 2022-07-16
?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

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



查看完整回答
反對(duì) 回復(fù) 2022-07-16
?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

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

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


查看完整回答
反對(duì) 回復(fù) 2022-07-16
?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

$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

    //....

)

現(xiàn)在您可以將$json變量用作數(shù)組:


echo $json['access_token'];

echo $json['token_type'];


查看完整回答
反對(duì) 回復(fù) 2022-07-16
  • 4 回答
  • 0 關(guān)注
  • 152 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)