Php Google Sheets API(“接收到无效的JSON负载。未知名称\";\:根元素必须是消息”)

Php Google Sheets API(“接收到无效的JSON负载。未知名称\";\:根元素必须是消息”),php,google-sheets-api,Php,Google Sheets Api,试图通过GoogleSheetsAPI和BatchUpdate方法将数据添加到GoogleSheet,但我收到了这个错误(不知道它指的是什么) 这是我的新代码: $spreadsheetId = 'myspreadsheetid'; $client = new Google_Client(); $client->setAuthConfig('mycredntials.json'); $client->setApplicationName('Sheet Automation')

试图通过GoogleSheetsAPI和BatchUpdate方法将数据添加到GoogleSheet,但我收到了这个错误(不知道它指的是什么)

这是我的新代码:

 $spreadsheetId = 'myspreadsheetid';
 $client = new Google_Client();
 $client->setAuthConfig('mycredntials.json');
 $client->setApplicationName('Sheet Automation');
 $client->setScopes(Google_Service_Sheets::SPREADSHEETS);
 $service = new Google_Service_Sheets($client);
 $range = 'A2:L';
 $valueInputOption = 'USER_ENTERED';
 $body = new Google_Service_Sheets_ValueRange([
            'values' => $row1
    ]);
 $params = [
      'valueInputOption' => $valueInputOption
    ];
 $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
编辑:

我从数据库中获取的数据被放入一个数组($row1),当回送时,该数组如下所示:

(
    [cdr_id] => myid
    [calldate] => 2021-05-27
    [src] => mysource
    [accountcode] => myaccountcode
    [userfield] => disposition
    [ivr_std_txn_full] =>
)
 $body = new Google_Service_Sheets_ValueRange([
                //'values' => $row1
                'values' =>
                [
                        [
                                $row1['cdr_id'], $row1['calldate'], $row1['src'], $row1['accountcode'], $row1['userfield'], $row1['ivr_std_txn_full']
                        ]
                ]
        ]);
        $params = [
          'valueInputOption' => $valueInputOption
        ];
        $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
然后我抓取这些信息并使用内爆将其全部放在一行中(这可能就是问题所在)


我尝试设置附加到$row1和$line1的值,但仍然存在有效负载问题。

我最终解决了这个问题后,解决方案非常简单。如果您不需要手动输入值,而是让一个数组充满数据,那么您只需要引用数组中的每个键,而不仅仅是数组

请求主体最终是这样的:

(
    [cdr_id] => myid
    [calldate] => 2021-05-27
    [src] => mysource
    [accountcode] => myaccountcode
    [userfield] => disposition
    [ivr_std_txn_full] =>
)
 $body = new Google_Service_Sheets_ValueRange([
                //'values' => $row1
                'values' =>
                [
                        [
                                $row1['cdr_id'], $row1['calldate'], $row1['src'], $row1['accountcode'], $row1['userfield'], $row1['ivr_std_txn_full']
                        ]
                ]
        ]);
        $params = [
          'valueInputOption' => $valueInputOption
        ];
        $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);

关于您的请求主体,我认为需要对其进行修改。但不幸的是,我无法从你的问题和剧本中理解你的目标。因此,我无法提出修改后的脚本。我为此道歉。我能问一下你的目标的细节吗?@Tanaike是的,基本上我是从数据库中获取数据,输入到谷歌表格中。这部分代码在这里没有显示,但$line1是包含我从数据库中获得的数据的数组。对于请求主体,我想做两件事:将数据追加到新行,并将F列中新追加的数据拆分为单独的列,因为这是一条用逗号分隔的长行,我需要从中获取一些数据,因此这样做是感谢您回复的最佳选择。我不得不为我糟糕的英语水平道歉。不幸的是,我无法从你的回复中想象你的目标。但我想试着去理解它。当我能正确理解它时,我想想出解决办法。非常抱歉,我不能很快解决您的问题。您好@uzzi!该错误声明JSON没有满足所需的格式。要丢弃错误配置的JSON,请您测试一个有效的已知JSON,看看它是否会产生错误?嘿@Jacques GuzelHeron,所以在稍微更改代码以开始简单操作并发现问题后,我的目标是添加新数据。即使如此,它仍然给我同样的问题,所以有效负载的问题是我的数据格式不正确。我编辑了我的问题和代码,并添加了更多关于请求中发送的数据的信息。希望它有助于识别问题。谢谢你的帮助