-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_curl_call.php
More file actions
608 lines (524 loc) · 25.6 KB
/
multi_curl_call.php
File metadata and controls
608 lines (524 loc) · 25.6 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
/*
File: multi_curl_call.php
Created: 07/22/2020
Updated: 07/23/2020
Programmer: Cuates
Updated By: Cuates
Purpose: Retrieve data from the database and process them in bulk via CURL
*/
// https://stackoverflow.com/questions/4368603/multiple-php-curl-posts-to-same-page
// https://stackoverflow.com/questions/11480763/how-to-get-parameters-from-a-url-string
// https://stackoverflow.com/questions/1355072/array-push-with-key-value-pair
// https://stackoverflow.com/questions/1834703/php-foreach-loop-key-value
// https://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking
// https://gist.github.com/anonymous/1a9eb381f6a5f260bd20
// https://snelling.io/curl-multi-high-cpu-usage/
// Include error check class
include ("checkerrorclass.php");
// Create an object of error check class
$checkerrorcl = new checkerrorclass();
// Set variables
$developerNotify = 'cuates@email.com'; // Production email(s)
// $developerNotify = 'cuates@email.com'; // Development email(s)
$endUserEmailNotify = 'cuates@email.com'; // Production email(s)
// $endUserEmailNotify = 'cuates@email.com'; // Development email(s)
$externalEndUserEmailNotify = ''; // Production email(s)
// $externalEndUserEmailNotify = 'cuates@email.com'; // Development email(s)
$scriptName = 'Multi Curl Call Ingestion'; // Production
// $scriptName = 'TEST Multi Curl Call Ingestion TEST'; // Development
$fromEmailServer = 'Email Server';
$fromEmailNotifier = 'email@email.com';
// Retrieve any other issues not retrieved by the set_error_handler try/catch
// Parameters are function name, $email_to, $email_subject, $from_mail, $from_name, $replyto, $email_cc and $email_bcc
register_shutdown_function(array($checkerrorcl,'shutdown_notify'), $developerNotify, $scriptName . ' Error', $fromEmailNotifier, $fromEmailServer, $fromEmailNotifier);
// Function to catch exception errors
set_error_handler(function ($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});
// Attempt script logic
try
{
// Set new memory limit Note: This will revert back to original limit upon end of script
ini_set('memory_limit', '4095M');
// // Set local
// setlocale(LC_ALL, "en_US.utf8");
// Declare download directory
define ('TEMPDOC', '/var/www/html/Temp_Directory/');
// Include class file
include ("multi_curl_class.php");
// Create an object of class as used by the API calls
$multi_curl_cl = new multi_curl_class();
// Initialize variables and arrays
$errorFilename = "";
$errorString = "";
$errorPrefixFilename = "multi_curl_issue_"; // Production
// $errorPrefixFilename = "multi_curl_dev_issue_"; // Development
$errormessagearray = array();
$foundArray = array();
$finalArray = array();
$finalFoundArray = array();
$idNum = 0; // Change number here for what the id number is in the database table
$idNumScript = 1; // Change number here for what the id number script is in the database table
$accountStatus = array('deleted');
$lineBreakString = array("\r\n", "\r", "\n");
// Extract Sequence date time
$extractTimeStamp = $multi_curl_cl->extractTimeStampDate($idNum);
// Check if server error
if (!isset($extractTimeStamp['SError']) && !array_key_exists('SError', $extractTimeStamp))
{
// Retrieve the date executed
$seqdatetime = reset($extractTimeStamp);
// Register (update) data into database table
$regUpdateDataInformation = $multi_curl_cl->updateDataInformation($seqdatetime);
// Explode database message
$registerUpdateData = explode('~', $regUpdateDataInformation);
// Set response message
$registerUpdateServerResp = reset($registerUpdateData);
$registerUpdateServerMesg = next($registerUpdateData);
// Check if error with registering process
if (trim($registerUpdateServerResp) !== "Success")
{
// Set array with error records for processing
array_push($errormessagearray, array('Update Data In Bulk', '', '', $seqdatetime, $idNum, $registerUpdateServerResp, $registerUpdateServerMesg));
}
// Register (insert) data into database table
$regInsertDataInformation = $multi_curl_cl->insertDataInformation($seqdatetime);
// Explode database message
$registerInsertData = explode('~', $regInsertDataInformation);
// Set response message
$registerInsertServerResp = reset($registerInsertData);
$registerInsertServerMesg = next($registerInsertData);
// Check if error with registering process
if (trim($registerInsertServerResp) !== "Success")
{
// Set array with error records for processing
array_push($errormessagearray, array('Insert Data In Bulk', '', '', $seqdatetime, $idNum, $registerInsertServerResp, $registerInsertServerMesg));
}
// Check if the registration has produced any issues
if (count($errormessagearray) <= 0)
{
// Update sequence date
$updateTimeStamp = $multi_curl_cl->updateDateTimeStamp($idNum);
// Explode database message
$updateTimeStampData = explode('~', $updateTimeStamp);
// Set response message
$updateTimeStampServerResp = reset($updateTimeStampData);
$updateTimeStampServerMesg = next($updateTimeStampData);
// Check if error with registering process
if (trim($updateTimeStampServerResp) !== "Success")
{
// Set array with error records for processing
array_push($errormessagearray, array('Update Date Time Stamp', '', '', $seqdatetime, $idNum, $updateTimeStampServerResp, $updateTimeStampServerMesg));
}
}
else
{
// Set array with error records for processing
array_push($errormessagearray, array('Update Date Time Stamp Not Updated', '', '', $seqdatetime, $idNum, 'Error', 'Was not Performed Due to Issue(s) in Register'));
}
}
else
{
// Else error has occurred
$extractTimeStampDataServerMesg = reset($extractTimeStamp);
// Append error message
array_push($errormessagearray, array('Extract Date Time Stamp Issue', '', '', '', $idNum, 'Error', $extractTimeStampDataServerMesg));
}
// Extract data
$extractDataResponse = $multi_curl_cl->extractData();
// Check if server error
if (!isset($extractDataResponse['SError']) && !array_key_exists('SError', $extractDataResponse))
{
// Check if there is data to process
if (count($extractDataResponse) > 0)
{
// Set parameter
$authToken = "";
// Authenticate call
$authAPI = $multi_curl_cl->authenticateAPI();
// Split string
$authAPIRespArray = explode('~', $authAPI);
// Set response and message
$authAPIResp = reset($authAPIRespArray);
$authAPIMesg = next($authAPIRespArray);
// Check if the authentication was successful
if ($authAPIResp === "Success")
{
// Decode JSON message
$authResponse = json_decode($authAPIMesg, true);
// Check if JSON was decoded properly
if (json_last_error() == JSON_ERROR_NONE)
{
// Authenticate
if (isset($authResponse["response"]["authToken"]))
{
// Set parameter
$authToken = $authResponse["response"]["authToken"];
}
else
{
// Set array with error records for processing
array_push($errormessagearray, array('API Authenticate Token Before Search', '', '', '', '', 'Error', str_replace($lineBreakString, '', $authAPIMesg)));
}
}
else
{
// Append error message
array_push($errormessagearray, array('JSON Issue Before Search', '', '', '', '', 'Error', str_replace($lineBreakString, '', $authAPIMesg)));
}
}
else
{
// Append error message
array_push($errormessagearray, array('Auth API Issue Before Search', '', '', '', '', 'Error', str_replace($lineBreakString, '', $authAPIMesg)));
}
// Check if auth token is not empty
if ($authToken !== "")
{
// Bulk search call
$searchAPIResponse = $multi_curl_cl->searchMultiAPI('?pg=1&pgsize=10', $authToken, $extractDataResponse);
// Drop the data and shrink stack memory almost immediately
$extractDataResponse = null;
$extractDataResponse = array();
// Check if server error was given when executing search call(s)
if (!isset($searchAPIResponse['SError']) && !array_key_exists('SError', $searchAPIResponse))
{
// Process all search messages from the curl calls
foreach($searchAPIResponse as $searchNumber => $searchAPIVals)
{
// Decode JSON message
$searchResponse = json_decode($searchAPIVals, true);
// Check if JSON was decoded properly
if (json_last_error() == JSON_ERROR_NONE)
{
// Total Count
if (isset($searchResponse["response"]["totalCount"]))
{
// Initialize parameter
$totalValueCount = $searchResponse["response"]["totalCount"];
// Check if value exist
if ($totalValueCount > 0)
{
// Result ID
if (isset($searchResponse["response"]["results"][0]["root"]["id"]))
{
// Check if the status exist within the return message
if (isset($searchResponse["response"]["results"][0]["root"]["account"]["status"]))
{
// Check if the status is good to proceed
if (!in_array(strtolower($searchResponse["response"]["results"][0]["root"]["account"]["status"]), $accountStatus))
{
// Store all fields in the response
$postFieldsArray = $searchResponse["response"]["results"][0];
// Store found searched curl calls into array for later processing
$foundArray[$searchNumber] = $postFieldsArray;
}
else
{
// Update the table to status of 10 which will mean there are other issues that occurred
$validateDataValidationStatusResponse = $multi_curl_cl->validateData('0', '10', $searchNumber);
// Explode database message
$validateDataValidationStatusReturn = explode('~', $validateDataValidationStatusResponse);
// Set response message
$validateDataValidationStatusServerResp = reset($validateDataValidationStatusReturn);
$validateDataValidationStatusServerMesg = next($validateDataValidationStatusReturn);
// Check if the validate status was successful
if ($validateDataValidationStatusServerResp !== 'Success')
{
// Set array with error records for processing
array_push($errormessagearray, array('Update Data Validate Status Issue', $searchNumber, '', '', '', 'Error', $validateDataValidationStatusServerMesg));
}
}
}
else
{
// Set array with error records for processing
array_push($errormessagearray, array('API Search Result Status Issue', $searchNumber, '', '', '', 'Error', str_replace($lineBreakString, '', $searchAPIVals)));
}
}
else
{
// Set array with error records for processing
array_push($errormessagearray, array('API Search Result ID Issue', $searchNumber, '', '', '', 'Error', str_replace($lineBreakString, '', $searchAPIVals)));
}
}
else if ($totalValueCount === 0)
{
// Do nothing as there was no match for the number
}
else
{
// Initialize parameter
$errorMessageReturn = "";
// Check if error message exist
if (isset($authResponse["response"]["errors"][0]))
{
$errorMessageReturn = $authResponse["response"]["errors"][0];
}
else
{
// Else unknown error occurred
$errorMessageReturn = $searchAPIVals;
}
// Set array with error records for processing
array_push($errormessagearray, array('API Search Count Issue', $searchNumber, '', '', '', 'Error', str_replace($lineBreakString, '', $errorMessageReturn)));
}
}
else
{
// Set array with error records for processing
array_push($errormessagearray, array('API Search Count Error', $searchNumber, '', '', '', 'Error', str_replace($lineBreakString, '', $searchAPIVals)));
}
}
else
{
// Append error message
array_push($errormessagearray, array('Search JSON Issue', $searchNumber, '', '', '', 'Error', str_replace($lineBreakString, '', $searchAPIVals)));
}
}
// Drop the data and shrink stack memory almost immediately
$searchAPIResponse = null;
$searchAPIResponse = array();
}
else
{
// Else error has occurred
$searchAPIResponseServerMesg = reset($searchAPIResponse);
// Append error message
array_push($errormessagearray, array('Search API Response', '', '', '', '', 'Error', $searchAPIResponseServerMesg));
}
}
// Call to database to build a JSON string per param 01 in array
foreach($foundArray as $foundSearchNumber => $foundSearchValue)
{
// Extract Param 01 data
$extractParam01DataResponse = $multi_curl_cl->extractParam01Data($foundSearchNumber);
// Check if server error
if (!isset($extractParam01DataResponse['SError']) && !array_key_exists('SError', $extractParam01DataResponse))
{
$jsonParam01 = isset($foundSearchValue['root']['jsonParam01']) ? trim($foundSearchValue['root']['jsonParam01']) : "";
$jsonParam01 = ($jsonParam01 === '1') ? true : $jsonParam01;
($jsonParam01 === "") ? : $foundSearchValue['root']['jsonParam01'] = $jsonParam01;
$numberString = reset($extractParam01DataResponse);
$jsonParam02 = next($extractParam01DataResponse);
$jsonParam02 = jsonParam02($jsonParam02);
(strtolower($jsonParam02) === "" ? : $foundSearchValue['root']['extensions']['jsonParam02'] = $jsonParam02);
$jsonParam03 = next($extractParam01DataResponse);
$jsonParam03 = isset($foundSearchValue['root']['extensions']['jsonParam03']) ? trim($foundSearchValue['root']['extensions']['jsonParam03']) : trim($jsonParam03);
$jsonParam03 = ($jsonParam03 === '1') ? true : $jsonParam03;
// Check if the value from the database is true
if ($jsonParam03 === 'true')
{
// Set to true
$jsonParam03 = true;
}
else if ($jsonParam03 === 'false')
{
// Check if the value from the database is false
// Set to false
$jsonParam03 = false;
}
(strtolower($jsonParam04) === "" ? : $foundSearchValue['root']['extensions']['jsonParam04'] = $jsonParam04);
// Set parameters
$postFieldsJSON = '';
$postFieldsJSON = '[' . json_encode($foundSearchValue) . ']';
// Push JSON encoded string into an array for later processing
array_push($finalArray, $postFieldsJSON);
}
else
{
// Else error has occurred
$extractOneDataServerMesg = reset($extractParam01DataResponse);
// Append error message
array_push($errormessagearray, array('Extract Param 01 Data Issue', $foundSearchNumber, '', '', '', 'Error', $extractOneDataServerMesg));
}
}
// Drop the data and shrink stack memory almost immediately
$foundArray = null;
$foundArray = array();
// Check if there is data to process
if (count($finalArray) > 0)
{
// Reset parameter
$authToken = "";
// Authenticate call
$authAPI = $multi_curl_cl->authenticateAPI();
// Split string
$authAPIRespArray = explode('~', $authAPI);
// Set response and message
$authAPIResp = reset($authAPIRespArray);
$authAPIMesg = next($authAPIRespArray);
// Check if the authentication was successful
if ($authAPIResp === "Success")
{
// Decode JSON message
$authResponse = json_decode($authAPIMesg, true);
// Check if JSON was decoded properly
if (json_last_error() == JSON_ERROR_NONE)
{
// Authenticate
if (isset($authResponse["response"]["authToken"]))
{
// Set parameter
$authToken = $authResponse["response"]["authToken"];
}
else
{
// Set array with error records for processing
array_push($errormessagearray, array('API Authenticate Token After Database Built', '', '', '', '', 'Error', str_replace($lineBreakString, '', $authAPIMesg)));
}
}
else
{
// Append error message
array_push($errormessagearray, array('Auth JSON Issue After Database Built', '', '', '', '', 'Error', str_replace($lineBreakString, '', $authAPIMesg)));
}
}
else
{
// Append error message
array_push($errormessagearray, array('Auth API Issue After Database Built', '', '', '', '', 'Error', str_replace($lineBreakString, '', $authAPIMesg)));
}
// Check if auth token is not empty
if ($authToken !== "")
{
// POST update via API call in bulk
$updateRootAPI = $multi_curl_cl->postUpdateDataMultiAPI($finalArray, $authToken);
// Drop the data and shrink stack memory almost immediately
$finalArray = null;
$finalArray = array();
// Check if server error was given when executing search call(s)
if (!isset($updateRootAPI['SError']) && !array_key_exists('SError', $updateRootAPI))
{
// Process all posted update messages from the curl calls
foreach($updateRootAPI as $updateRootNumber => $updateRootValue)
{
// Decode JSON message
$updateRootResponse = json_decode($updateRootValue, true);
// Check if JSON was decoded properly
if (json_last_error() == JSON_ERROR_NONE)
{
// Check if id was returned from the JSON string
if (isset($updateRootResponse["response"]["results"][0]["root"]["id"]))
{
// Store posted value into array for later processing
$finalFoundArray[$updateRootNumber] = $updateRootValue;
// Update the table with the new information
$validateDataValidationResponse = $multi_curl_cl->validateData('0', '3', $updateRootNumber);
// Explode database message
$validateDataValidationReturn = explode('~', $validateDataValidationResponse);
// Set response message
$validateDataValidationServerResp = reset($validateDataValidationReturn);
$validateDataValidationServerMesg = next($validateDataValidationReturn);
// Check if the validate status was successful
if ($validateDataValidationServerResp !== 'Success')
{
// Set array with error records for processing
array_push($errormessagearray, array('Update Data Validate Issue', $updateRootNumber, '', '', '', 'Error', $validateDataValidationServerMesg));
}
}
else
{
// Set array with error records for processing
array_push($errormessagearray, array('API Update Root ID Error', $updateRootNumber, '', '', '', 'Error', str_replace($lineBreakString, '', $updateRootValue)));
}
}
else
{
// Append error message
array_push($errormessagearray, array('Update Root JSON Issue', $updateRootNumber, '', '', '', 'Error', str_replace($lineBreakString, '', $updateRootValue)));
}
}
// Drop the data and shrink stack memory almost immediately
$updateRootAPI = null;
$updateRootAPI = array();
}
else
{
// Else error has occurred
$updateRootAPIResponseServerMesg = reset($updateRootAPI);
// Append error message
array_push($errormessagearray, array('POST Update Root API Response', '', '', '', '', 'Error', $updateRootAPIResponseServerMesg));
}
}
}
}
}
// Update sequence date
$sequenceUpdate = $multi_curl_cl->updateDateTimeStamp($idNumScript);
// Explode database message
$sequenceUpdateData = explode('~', $sequenceUpdate);
// Set response message
$updateSeqServerResp = reset($sequenceUpdateData);
$updateSeqServerMesg = next($sequenceUpdateData);
// Check if error with updating sequence
if (trim($updateSeqServerResp) !== "Success")
{
// Set array with error records for processing
array_push($errormessagearray, array('Update Data Sequence', '', '', '', $idNumScript, $updateSeqServerResp, $updateSeqServerMesg));
}
// Check if error message array is not empty
if (count($errormessagearray) > 0)
{
// Set prefix file name and headers
$errorFilename = $errorPrefixFilename . date("Y-m-d_H-i-s") . '.csv';
$colHeaderArray = array(array('Process', 'Number', 'Post Field String', 'Sequence Date Time', 'Sequence Number', 'Response', 'Message'));
// Initialize variable
$to = "";
$to = $developerNotify;
$to_cc = "";
$to_bcc = "";
$fromEmail = $fromEmailNotifier;
$fromName = $fromEmailServer;
$replyTo = $fromEmailNotifier;
$subject = $scriptName . " Error";
// Set the email headers
$headers = "From: " . $fromName . " <" . $fromEmail . ">" . "\r\n";
// $headers .= "CC: " . $to_cc . "\r\n";
// $headers .= "BCC: " . $to_bcc . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
// $headers .= "X-Priority: 3\r\n";
// Mail priority levels
// "X-Priority" (values: 1, 3, or 5 from highest[1], normal[3], lowest[5])
// Set priority and importance levels
$xPriority = "";
// Set the email body message
$message = "<!DOCtype html>
<html>
<head>
<title>"
. $scriptName .
" Error
</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<!-- Include next line to use the latest version of IE -->
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\" />
</head>
<body>
<div style=\"text-align: center;\">
<h2>"
. $scriptName .
" Error
</h2>
</div>
<div style=\"text-align: center;\">
There was an issue with " . $scriptName . " Error process.
<br />
<br />
Do not reply, your intended recipient will not receive the message.
</div>
</body>
</html>";
// Call notify developer function
$multi_curl_cl->notifyDeveloper(TEMPDOC, $errorFilename, $colHeaderArray, $errormessagearray, $to, $to_cc, $to_bcc, $fromEmail, $fromName, $replyTo, $subject, $headers, $message, $xPriority);
}
}
catch(Exception $e)
{
// Call to the function
$checkerrorcl->caught_error_notify($e, $developerNotify, $scriptName . ' Error', $fromEmailNotifier, $fromEmailServer, $fromEmailNotifier);
}
?>