Docs: https://www.php.net/manual/en/ref.json.php
- json_decode — Decodes a JSON string
- json_encode — Returns the JSON representation of a value
- json_last_error — Returns the last error occurred
- json_last_error_msg — Returns the error string of the last json_encode() or json_decode() call
- json_validate — Checks if a string contains valid JSON
Example
$url = "https://docs.google.com/spreadsheets/d/$id/gviz/tq?tqx=out:json&gid=$gid";
$json = file_get_contents($url);
if ($json === false) {
error_log("[Page Import] ❌ Ошибка загрузки данных из Google Sheets: $url");
return;
}
$jsonTrimmed = substr($json, 47, -2);
$data = json_decode($jsonTrimmed, true);
if (json_last_error() !== JSON_ERROR_NONE) {
error_log("[Page Import] ❌ Ошибка JSON: " . json_last_error_msg());
return;
}
$rows = $data['table']['rows'] ?? [];
$columns = $data['table']['cols'] ?? [];