php file_put_contents 从 Google Sheet 获取数据并在 WordPress 网站上保存为 CSV 文件

问题描述 投票:0回答:1

我的 php 经验为零,所以我的代码/问题纯粹基于谷歌搜索。如果你们都对我的代码感到畏缩,我提前道歉。

我创建了一个 php 脚本,每 5 分钟通过 cron 运行一次,以从共享的公共 Google Sheet 获取数据,并将其作为 CSV 文件保存在我的网站服务器(共享主机、Wordpress)上

脚本运行良好,但千载难逢,我遇到了一些错误...例如:它可以运行 1 小时,但接下来的一个小时我得到了奇怪的错误,所有错误都与“无法打开流”相关:

  1. HTTP 请求失败! HTTP/1.0 500 内部服务器错误

  2. HTTP 请求失败! HTTP/1.0 400 错误请求

  3. HTTP 请求失败! [无代码]

我认为 2 和 3 纯粹是我的服务器连接到 Google Sheet 时发生的通信错误?

我认为 1 只是我的服务器的一个错误。

我在代码中添加了一些注释,说明我要对每个步骤执行的操作...

效果很好,我很满意。我只是想知道我是否可以消除错误和/或(我的问题的第二部分)是否有人对如何改进代码有一些评论(请残酷)...我确信我可以使用循环而不是重复 1600代码行。

此外,当我检查服务器资源时,脚本几乎没有任何影响,因此有大量可用的服务器资源。

<?php

/// Global variables
$fileid = "2PACX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Google Sheet ID from File--> Share--> Publish to Web.
$save_location_Data = "/home/[ my-site ]/wp-content/uploads/database"; // where I am saving the CSV files on my server

/**
 * MEN.
 */
$division = "MEN";
$sheet_[$division] = "673108837"; // Google Sheet, Tab ID

$datatype = "Teams";
$range_[$division][$datatype] = "range=A1:B200"; // Selected range from tab
$url_[$division][$datatype] = "https://docs.google.com/spreadsheets/d/e/".$fileid."/pub?gid=".$sheet_[$division]."&".$range_[$division][$datatype]."&single=true&output=csv";
$filename_[$division][$datatype] = ($save_location_Data."/".$division."-".$datatype."_temp_.csv"); // I am just building the full URL here.
file_put_contents($filename_[$division][$datatype], file_get_contents($url_[$division][$datatype]));
clearstatcache();
if(filesize($filename_[$division][$datatype]) != 0) {
    rename($filename_[$division][$datatype], $save_location_Data."/".$division."-".$datatype.".csv");
    }
    else {
        unlink($filename_[$division][$datatype]);
}
// Just checking if the data is 0 bytes, if no, rename temp file to final file. If yes, delete. 
// I thought saving a temp file first would be quicker (more reliable) rather than file_put_contents to final file. 
// If the CSV data is requested by my website (in a table), it's only milliseconds the CSV file is locked to write new data so less chance of being "unavailable".

$datatype = "Stats";
$range_[$division][$datatype] = "range=C1:H200";
$url_[$division][$datatype] = "https://docs.google.com/spreadsheets/d/e/".$fileid."/pub?gid=".$sheet_[$division]."&".$range_[$division][$datatype]."&single=true&output=csv";
$filename_[$division][$datatype] =."/".$division."-".$datatype."_temp_.csv");
file_put_contents($filename_[$division][$datatype], file_get_contents($url_[$division][$datatype]));
clearstatcache();
if(filesize($filename_[$division][$datatype]) != 0) {
    rename($filename_[$division][$datatype]."/".$division."-".$datatype.".csv"); 
    }
    else {
        unlink($filename_[$division][$datatype]);
}
$datatype = "Games_1";
$range_[$division][$datatype] = "range=I1:J200";
$url_[$division][$datatype] = "https://docs.google.com/spreadsheets/d/e/".$fileid."/pub?gid=".$sheet_[$division]."&".$range_[$division][$datatype]."&single=true&output=csv";
$filename_[$division][$datatype] =."/".$division."-".$datatype."_temp_.csv");
file_put_contents($filename_[$division][$datatype], file_get_contents($url_[$division][$datatype]));
clearstatcache();
if(filesize($filename_[$division][$datatype]) != 0) {
    rename($filename_[$division][$datatype]."/".$division."-".$datatype.".csv");
    }
    else {
        unlink($filename_[$division][$datatype]);
}

// The above code repeats about 100+ times to create 120x CSV files.
// Total size of 120x CSV files is only about 60-70kb so not much data really
// I wonder if I need to repeat the code? I am sure I could use a loop?
// I wonder too if I am mixing strings with arrays accidentally

?>
php google-sheets
1个回答
0
投票

我稍微简化了你的代码。 它应该与您的代码执行相同的操作 - 但尚未经过测试! 请尝试一下是否有效。

<?php

/// Global variables
$fileid = "2PACX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Google Sheet ID from File--> Share--> Publish to Web.
$save_location_Data = "/home/[ my-site ]/wp-content/uploads/database"; // where I am saving the CSV files on my server

/**
 * MEN.
 */
$division = "MEN";
$sheet = "673108837"; // Google Sheet, Tab ID

$datatype[];    //make empty array
$range[];
$datatype[0] = "Teams"
$range[0] = "range=A1:B200"
$datatype[1] = "Stats";
$range[1] = "range=C1:H200";
$datatype[2] = "Games_1";
$range[2] = "range=I1:J200";
//you could use arraypush -> so you do not need declaring the arrayindex
//eg..
//array_push($datatype, "Games_99");
//array_push($range, "range=X1:YJ200");

//atlernatively you could use a multidimensional array - but for now we use 2 arrays and make suer the indexes corespond

for ($i=0; $i < count($datatype); $i++) {
    $url = "https://docs.google.com/spreadsheets/d/e/".$fileid."/pub?gid=".$sheet."&".$range[i]."&single=true&output=csv";
    $filename =."/".$division."-".$datatype[i]."_temp_.csv";
    file_put_contents($filename, file_get_contents($url));
    clearstatcache();
    if(filesize($filename) != 0) {
        rename($filename."/".$division."-".$datatype.".csv"); 
        }
        else {
            unlink($filename);
    }
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.