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

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

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

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'];
- 4 回答
- 0 關(guān)注
- 152 瀏覽
添加回答
舉報(bào)