I have ZERO php experience, so my code/problem is purely based off Googling. I apologize in advance if you all cringe at my code.
I have created a php script which I run via cron every 5min to get data from a shared public, Google Sheet and save it as CSV files on my website’s server (Shared Hosting, WordPress)
The script runs fine but once in a blue moon I get a couple of errors… EG: It could run for 1 hour, nothing but the next hour I get the odd error all are "failed to open stream" related:
-
HTTP request failed! HTTP/1.0 500 Internal Server Error
-
HTTP request failed! HTTP/1.0 400 Bad Request
-
HTTP request failed! [ No code ]
I assume 2 and 3 is purely just a communication error with my server connecting to the Google Sheet?
I assume 1 is just an error by my server.
I’ve added some comments in my code of what I am trying to do with each step…
It works fine, I am happy with it. I just wonder if I can eliminate the errors and / or (2nd part of my question) if anyone has some comments (please be brutal) on how to improve the code… I’m sure I can use loops instead of repeating 1600 lines of code.
Also, when I check my server resources, the script is having next to no impact so plenty of server resources available.
<?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
?>
2
Answers
OK, thanks to ratmalwer I figured out how to create some more efficient code. He did 99% of it (had a couple of typos but that's fine)
My new code below. I added str_replace("&","&",$url) to hopefully take care of those odd occasions whereby I end up with "&" instead of "&" in my $url. I will run the script for a few hours and see if I get any HTTP request failed! errors.
Hope this helps someone else: "Grab (specified) data from certain columns in a Public Google Sheet and save it as a CSV file in "a location". But this cycles through multiple columns and multiple sheets (tabs in the same Google Sheet)
I simplified your code a little bit.
It should do the same as your code – but it is not tested!
Please try if it works.