【问题标题】:How to parse Google Fit Json String with PHP如何使用 PHP 解析 Google Fit Json 字符串
【发布时间】:2020-04-24 22:17:27
【问题描述】:

我正在尝试解析我从 GoogleFit API 获得的响应。这是我写的sn-p:

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;

响应如下所示:

{ "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 行是记录而不是响应。

true

我想将access_tokenrefresh_tokenexpires_in 存储在我的db 中。我也无法访问响应的属性。请帮忙

【问题讨论】:

  • curl_exec() 默认返回truefalse。你改变了这种行为吗?
  • 请问我应该如何将 curl_exec() 更改为 @ÁlvaroGonzález??

标签: php json laravel rest google-fit


【解决方案1】:

谢谢,为这个帖子做出贡献的每一个人。但是,我在网上找到了一个让我更轻松的图书馆,Google API PHP Client

【讨论】:

    【解决方案2】:

    某些 API 以无效的 JSON 响应。出于安全原因,他们在 JSON 对象之后添加了一个布尔表达式(true 或 1)。在解析之前,您可能必须自己预先处理响应。

    【讨论】:

    • 出于安全原因输出无效?你有任何链接可以备份吗?
    • 是的,发生的事情是布尔响应被附加到响应中,但我不知道如何处理,请帮助
    【解决方案3】:

    您可以通过以下方式解码/解析 JSON 响应:

    1. object
    2. PHP 关联数组

    对于第二个选项,在json_decode() 中使用true

    即您可以使用以下内容:

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

    工作demo

    【讨论】:

    • 这应该可以,但问题是1 被附加到响应中[如此处所示][1] [1]:3v4l.org/71W7r
    • 使用例如rtrim()demo
    • 你得到 1 的原因是你的 API 重新运行重定向响应而不是有效的 JSON 数据
    【解决方案4】:
    $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'];
    

    【讨论】:

    • 返回:{ "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 和 echo $json['access_token'];是空字符串
    • 能否将您的 API URL 粘贴到此处,以便我试一试?
    • 你发给我的链接返回一个 302 HTTP 响应码(重定向响应)
    • 请再试一次
    【解决方案5】:

    我假设您正在为您的日志记录编码 $result。之后,您可以使用json_decode($newResult, true) - 它基本上将其转换为数组,您可以获得所需的相关值。

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

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多