-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairtable-zapier-integration.php
226 lines (185 loc) · 6.1 KB
/
airtable-zapier-integration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?
if (isset($_GET['ping'])) { echo 'syntax ok'; exit; }
/*
===================================
Modification Log (recent entries):
===================================
2017-04-17 aaronstg - initial version
*/
//phpinfo();
//exit;
set_time_limit(0);
//Exit Script if HTTP_USER_AGENT is not Zapier
if($_SERVER["HTTP_USER_AGENT"] != 'Zapier') {exit;}
//PHP Variable Creation
foreach ($_GET as $key => $val) {
$$key = $val;
//echo $key. " => " . $val;
//echo "<br>";
}
//Email Data
$to = "email@examplesite.com";
$from = $_SERVER[SCRIPT_NAME];
$subject = "PHP Zapier Tool";
$edata = "";
//Functions
function send_mail($subject, $edata, $from, $to){
$message = 'From: ' . $from . "\t\n" . $edata;
$email = 'noreply@efilefreeonline.com';
$headers = 'From: ' . $email . "\t\n" .
'Reply-To: ' . $email . "\t\n" .
'X-Mailer: PHP/' . phpversion();
$sendmail = mail ($to, $subject, $message, $headers);
return $sendmail;
}
function curl_call_to_webhook($url, $postData){
/* START - ZAP CALL */
// Initialize curl
$curl = curl_init();
$opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array('Content-Type: application/json','Content-Length: ' . strlen($postData))
);
// Set curl options
curl_setopt_array($curl, $opts);
// Get the results
$result = curl_exec($curl);
// Close resource
curl_close($curl);
return $result;
/* END - ZAP CALL */
}
//Script Data
$title = "PHP Zapier Tool";
$curl = curl_init();
$timeout=5;
//Main Airtable View
$url = 'https://api.airtable.com/v0/app2AdJfucaKUslbm/Projects/?view=Main%20View&api_key={API_KEY}';
//Project Airtable View
//$url = 'https://api.airtable.com/v0/app2AdJfucaKUslbm/Projects/reclkIbsyApHFTNuj?api_key={API_KEY}';
// Initialize curl to get Airtable database information
curl_setopt($curl,CURLOPT_URL,$url);
$ua_arr = array('Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)','Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1', 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FSL 7.0.5.01003)');
$ref_arr = array('http://www.examplereferrer.com');
$index=rand(0,4);
curl_setopt($curl, CURLOPT_USERAGENT, $ua_arr[$index]);
$index=rand(0,4);
curl_setopt($curl, CURLOPT_REFERER, $ref_arr[$index]);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$timeout);
$response = curl_exec($curl);
curl_close($curl);
if ($response === false) {
die('Error '. curl_errno($curl) .': '. curl_error($curl));}
$response_decode = json_decode($response, true);
/* FOR TESTING
echo "<pre>";
print_r($response_decode);
echo "</pre>";
*/
//Work Type Array
$workType = array ('SEO Program','Paid Program','SCOPE','CORE');
$x = 0;
foreach ($response_decode as $records) {
foreach ($records as $rows) {
//Determine Inflow Work Type
$run = false;
foreach($records[$x]['fields']['Work Type'] as $key => $val) {
if(!$run) {
//echo "<br>".$key." ".$val."<br>";
if(in_array($val,$workType)) { $run = true; break; } else { $run = false; }
}
}
switch ($check) {
case "budget":
//Budget Check
if(isset($records[$x]['fields']['Slack Channel']) &&
((strtotime($records[$x]['fields']['Budget -Next Date To Discuss']) < strtotime('+1 month')) ||
!isset($records[$x]['fields']['Budget -Next Date To Discuss']))) {
if($run) {
if(isset($details)) {
echo "<pre>";
print_r($records[$x]);
echo "</pre>";
}
//JSON payload
$postData = json_encode($records[$x]);
if(isset($details)) {
echo "JSON payload: ".$postData."<br><br>";
}
//Zapier webhook url
if(isset($test)) {
$zapurl = 'https://hooks.zapier.com/hooks/catch/XXXX/XXXX/';
} else {
$zapurl = 'https://hooks.zapier.com/hooks/catch/XXXX/XXXX/';
}
//Curl Call to webhook
if(isset($update)){
//$edata .= "\t\n\t\n".$records[$x]['fields']['Slack Channel']."\t\n\t\n";
$curlCall = curl_call_to_webhook($zapurl, $postData);
}
if(isset($details)) {
echo "webhook result: ".$curlCall;
}
//exit;
}
}
break;
case "warmth":
//Warmth Check
if(isset($records[$x]['fields']['Slack Channel']) &&
((strtotime($records[$x]['fields']['Date Of Last Warmth Gesture']) < strtotime('-6 month')) ||
!isset($records[$x]['fields']['Date Of Last Warmth Gesture']))){
if($run) {
if(isset($details)) {
echo "<pre>";
print_r($records[$x]);
echo "</pre>";
}
//JSON payload
$postData = json_encode($records[$x]);
if(isset($details)) {
echo "JSON payload: ".$postData."<br><br>";
}
//Zapier webhook url enter your custom webhooks here
if(isset($test)) {
$zapurl = 'https://hooks.zapier.com/hooks/catch/XXXX/XXXX/';
} else {
$zapurl = 'https://hooks.zapier.com/hooks/catch/XXXX/XXXX/';
}
//Curl Call to webhook
if(isset($update)){
//$edata .= "\t\n\t\n".$records[$x]['fields']['Slack Channel']."\t\n\t\n";
$curlCall = curl_call_to_webhook($zapurl, $postData);
}
if(isset($details)) {
echo "webhook result: ".$curlCall;
}
//exit;
}
}
break;
}
$x++;
}
}
//Send Debug Email
$serverVariables = '';
$sessionVariables = '';
foreach($_SERVER as $key => $val) {
$serverVariables .= "Variable: ".$key." Value:".$val."\t\n";
}
foreach($_SESSION as $key => $val) {
$sessionVariables .= "Variable: ".$key." Value:".$val."\t\n";
}
$scriptData = "\t\n\t\nServer Variables:\t\n\t\n".$serverVariables."\t\n"."Session Variables:".$sessionVariables."\t\n\t\n";
if(isset($details)) {
echo $scriptData;
}
$edata .= $scriptData;
$mailsent=send_mail($subject, $edata, $from, $to);
?>