You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP报错:Cannot use object of type stdClass as array 求助

问题解决方案

1. 修复核心错误:对象转数组

报错原因是json_decode($response)默认返回stdClass对象,但你用了数组索引的方式访问数据。只需给json_decode添加第二个参数true,让它返回关联数组:

$data = json_decode($response, true);

2. 修正Google Sheets API请求URL

你当前的请求仅获取了电子表格的元数据,不是实际的单元格数据。需要修改URL指定要读取的工作表和数据范围,比如假设你的工作表名为Sheet1,修改后的URL如下:

// 替换Sheet1为你的实际工作表名称
$sheet_url = 'https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxxx/values/Sheet1!A:Z';

3. 正确处理API返回的数据结构

Google Sheets API返回的实际行数据在values字段中,需要先提取该数组,再将表头与行数据关联成键值对(方便用$datapoint['datum']这类索引访问):

// 检查是否获取到有效数据
if (!isset($data['values']) || empty($data['values'])) {
    die('未获取到表格数据,请检查API密钥和表格ID');
}

// 提取表头(第一行)
$headers = array_shift($data['values']);
$rows = $data['values'];

$html = file_get_contents('test.html');
// 遍历行数据
foreach ($rows as $row) {
    // 将行数据与表头关联为关联数组
    $datapoint = array_combine($headers, $row);
    // 匹配今日数据
    if ($datapoint['datum'] == date('d.m')) {
        $html = str_replace('{{IME}}', $datapoint['Ime'], $html);
        $html = str_replace('{{PREZIME}}', $datapoint['Prezime'], $html);
        $html = str_replace('{{ULICA}}', $datapoint['Ulica'], $html);
        $html = str_replace('{{BROJ}}', $datapoint['E-mail'], $html);
        // 如果只需要第一个匹配的今日数据,取消注释下面的break
        // break;
    }
}

4. 增加错误处理(可选但推荐)

为避免请求或解析失败导致的白屏,添加curl和JSON解析的错误检查:

// 检查curl请求是否成功
if(curl_errno($ch)) {
    die('Curl请求错误: ' . curl_error($ch));
}

// 检查JSON解析是否成功
if(json_last_error() !== JSON_ERROR_NONE) {
    die('JSON解析失败: ' . json_last_error_msg());
}

完整修正代码

<?php

// Vaš Google Sheets API ključ
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// URL vašeg Google Sheet-a + 工作表范围(替换Sheet1为实际名称)
$sheet_url = 'https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxxx/values/Sheet1!A:Z';

// Kreiranje URL-a za zahtev
$request_url = $sheet_url . '?key=' . $api_key;

// Povezivanje sa Google Sheets API-jem pomoću cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

// 检查curl请求错误
if(curl_errno($ch)) {
    die('Curl请求错误: ' . curl_error($ch));
}
curl_close($ch);

// Parsiranje JSON odgovora u asocijativni niz
$data = json_decode($response, true);

// 检查JSON解析错误
if(json_last_error() !== JSON_ERROR_NONE) {
    die('JSON解析失败: ' . json_last_error_msg());
}

// 检查是否获取到有效表格数据
if (!isset($data['values']) || empty($data['values'])) {
    die('未获取到有效表格数据');
}

// 提取表头和行数据
$headers = array_shift($data['values']);
$rows = $data['values'];

// Popunjavanje podataka u HTML dokument
$html = file_get_contents('test.html');
$found = false;

foreach ($rows as $row) {
    $datapoint = array_combine($headers, $row);
    if ($datapoint['datum'] == date('d.m')) {
        $html = str_replace('{{IME}}', $datapoint['Ime'], $html);
        $html = str_replace('{{PREZIME}}', $datapoint['Prezime'], $html);
        $html = str_replace('{{ULICA}}', $datapoint['Ulica'], $html);
        $html = str_replace('{{BROJ}}', $datapoint['E-mail'], $html);
        $found = true;
        // 仅需第一个匹配数据则取消注释
        // break;
    }
}

// 未找到今日数据时的提示
if (!$found) {
    $html = str_replace(['{{IME}}', '{{PREZIME}}', '{{ULICA}}', '{{BROJ}}'], 'Nema podataka za danas', $html);
}

// Postavljanje HTTP zaglavlja za datoteku
header('Content-Disposition: attachment; filename="popunjeno.html"');

// Prikazivanje HTML dokumenta sa popunjenim podacima
echo $html;

?>

注意事项

  • 确保你的Google Sheet已设置为公开可查看,或API密钥拥有该表格的访问权限
  • 表格中datum列的日期格式必须与date('d.m')输出格式一致(如15.08)
  • 替换代码中的xxxxxxxxxxxxx为你的实际表格ID,Sheet1为实际工作表名称

内容的提问来源于stack exchange,提问作者ZeusDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 20:05:29