1

I created this peace of PHP code to connect to googlew sheet and fill html file on my server:

 <?php

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

// URL vašeg Google Sheet-a
$sheet_url = 'https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxxx';

// 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_close($ch);

// Parsiranje JSON odgovora
$data = json_decode($response);

// Popunjavanje podataka u HTML dokument
$html = file_get_contents('test.html');
foreach ($data as $datapoint) {
    // Provjera da li se datum u koloni podudara sa trenutnim datumom
    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);
    }
}

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

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

?>

But I got this error: Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /home/xxxxxxx/public_html/sheet2html.php:26 Stack trace: #0 {main} thrown in /home/xxxxxxx/public_html/sheet2html.php on line 26

This script should populate HTML file from google sheet on with data from today only.

Maybe because it is late, maybe my knowledge is limited, I googled couple og hours, but mainly found this errors in JSON calls for example. Any help appreciated

1 Answer 1

2

I'm not exactly sure what's in the google JSON response, but chances are it's being decoded into an instance of stdClass by this line:

$data = json_decode($response);

if $data is an instance of stdClass, you can' use foreach on it unless you cast it as an array. Fortunately, json_decode allows you to provide an additional second parameter, $associative to force it to decode the JSON object as associative arrays (see arrays ) instead of stdClass objects. The documentation:

associative

When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects. When null, JSON objects will be returned as associative arrays or objects depending on whether JSON_OBJECT_AS_ARRAY is set in the flags.

So you could change your line of code to this:

$data = json_decode($response, TRUE);

And hopefully that will let you iterate through $data using the foreach construct.

Sign up to request clarification or add additional context in comments.

2 Comments

Great man, that was that, I really apreciate your help, kudos to you!
"if $data is an instance of stdClass, you can' use foreach on it" Actually, you can. php.net/manual/en/language.oop5.iterations.php The problem here was not that $data was an object but that $datapoint was an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.