Browse Source

php that gets the events from google sheets

carlos.diaz44 4 years ago
parent
commit
6f5ec05ed0
1 changed files with 97 additions and 0 deletions
  1. 97
    0
      www/Server/calendartoserver.php

+ 97
- 0
www/Server/calendartoserver.php View File

@@ -0,0 +1,97 @@
1
+<?php
2
+$json_url = "http://gsx2json.com/api?id=1MxnHv59agY8gElOnaTfC34LfdIV85OwwE6CQsZXqmrA&sheet=1";
3
+$json = file_get_contents($json_url);
4
+#decode gets the json file and turns it into a php array
5
+$data = json_decode($json, TRUE);
6
+#json gives data in rows and in colums, you want it in rows
7
+$json = $data['rows'];
8
+print_r($data);
9
+echo gettype(date("m/d/y"));
10
+
11
+if (file_exists('calendar.json')){
12
+	$serverjson = fopen('calendar.json', "r");
13
+	$serverjson = fread($serverjson,filesize("calendar.json"));
14
+	$serverjson = json_decode($serverjson,TRUE);
15
+	//if the jsons are not the same
16
+	if($serverjson!=$json){
17
+		$serverjson = $json;
18
+		$serverjson = json_encode($serverjson,TRUE);
19
+		//puts the new updated json in the server
20
+		unlink('calendar.json');
21
+		$filename = 'calendar.json';
22
+		$newfile = fopen($filename,'w');
23
+		fwrite($newfile,$serverjson);
24
+		fclose($newfile);
25
+		//Puts the day it was updated, month/day/year
26
+		$filename = ('updatedcalendar.txt');
27
+		$newfile = fopen($filename,'w');
28
+		$date = date("m/d/y");
29
+		$time = date("h:i:sa"); 
30
+		fwrite($newfile,"$date $time");
31
+		fclose($newfile);
32
+	}
33
+}
34
+
35
+	#Two cases, in case the modified bit is 1 or 0 (will be diferent from the other one because it was already made with modified bit 1)
36
+	#If it had modified bit 0 then try to chage its modified bit temporarily to compare
37
+	// if ($serverjson['modified'] == 1){
38
+	// 	$serverjson = json_encode($serverjson);
39
+	// 	#if the jsons are not the same then it replaces the file
40
+	// 	if ($serverjson != $json){
41
+	// 		unlink('calendar.json');
42
+	// 		#Make a new json with what you have and make a new file that is called calendar.json
43
+	// 		$filename = 'calendar.json';
44
+	// 		$newfile = fopen($filename,'w');
45
+	// 		fwrite($newfile,$json);
46
+	// 		fclose($newfile);
47
+	// 	} 
48
+
49
+	// }
50
+// 	else{
51
+// 		$serverjson['modified'] = 1;
52
+// 		$serverjson = json_encode($serverjson);
53
+// 		if ($serverjson != $json){
54
+// 			unlink('calendar.json');
55
+// 			#Make a new json with what you have and make a new file that is called calendar.json
56
+// 			$filename = 'calendar.json';
57
+// 			$newfile = fopen($filename,'w');
58
+// 			fwrite($newfile,$json);
59
+// 			fclose($newfile);
60
+// 		}
61
+// 	}
62
+
63
+// 	#if the jsons are not the same then it replaces the file
64
+// 	if ($serverjson != $json){
65
+// 		unlink('calendar.json');
66
+// 		#Make a new json with what you have and make a new file that is called calendar.json
67
+// 		$filename = 'calendar.json';
68
+// 		$newfile = fopen($filename,'w');
69
+// 		fwrite($newfile,$json);
70
+// 		fclose($newfile);
71
+// 	} 
72
+
73
+// }
74
+#--------------------------------------------------------------
75
+#print_r($data);
76
+#counts the amount of events in the google sheets that Ill use to compare with the ones in the server
77
+#echo count($data);
78
+#first one of the festival names
79
+#echo ($data[0]['nombredefestival']);
80
+#This to access the modified bit
81
+#echo ($data['modified'])
82
+#--------------------------------------------------------------
83
+else{
84
+	$filename = 'calendar.json';
85
+	$newfile = fopen($filename,'w');
86
+	fwrite($newfile,$json);
87
+	fclose($newfile);
88
+	//Puts the day it was updated, month/day/year
89
+	$filename = ('updatedcalendar.txt');
90
+	$newfile = fopen($filename,'w');
91
+	$date = date("m/d/y");
92
+	$time = date("h:i:sa"); 
93
+	fwrite($newfile,"$date $time");
94
+	fclose($newfile);
95
+	}
96
+
97
+?>