-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfunctions-utility.php
102 lines (68 loc) · 2.85 KB
/
functions-utility.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
<?php
// Function to use curl to get data from an API
// $headers should be an array for the actual headers to be used or omitted entirely
function curl_get_contents($curlUrl,$headers = NULL) {
// Initiate the curl session
$curlHandler = curl_init();
// Set the URL
curl_setopt($curlHandler, CURLOPT_URL, $curlUrl);
// Check if headers were null or given. If given then set the headers. Otherwise don't.
if($headers !== NULL) {
// Set the headers
curl_setopt($curlHandler, CURLOPT_HTTPHEADER, $headers);
}
// Removes the headers from the output
curl_setopt($curlHandler, CURLOPT_HEADER, 0);
// Return the output instead of displaying it directly
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl session
$curlOutput = curl_exec($curlHandler);
// Close the curl session
curl_close($curlHandler);
// Return the output as a variable
return $curlOutput;
}
// Function to use curl to post data for API responses
// $headers should be an array for the actual headers to be used or omitted entirely
// $data should be an array with the actual data to be used or omitted entirely
function curl_post($curlUrl,$headers = NULL, $data = NULL) {
// Initiate the curl session
$curlHandler = curl_init();
// Set the URL
curl_setopt($curlHandler, CURLOPT_URL, $curlUrl);
// Check if headers were null or given.
// If given then set the headers. Otherwise don't.
if($headers !== NULL) {
// Set the headers
curl_setopt($curlHandler, CURLOPT_HTTPHEADER, $headers);
}
if($data !== NULL) {
// Set the data
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $data);
}
// Removes the headers from the output
curl_setopt($curlHandler, CURLOPT_HEADER, 0);
// Post settings
curl_setopt($curlHandler, CURLOPT_POST, TRUE);
// Return the output instead of displaying it directly
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl session
$curlOutput = curl_exec($curlHandler);
// Close the curl session
curl_close($curlHandler);
// Return the output as a variable
return $curlOutput;
}
// Function to be able to splice items into arrays while preserving associative keys
// Use similar to array_splice with regards to parameters to enter
function array_splicek(&$array, $offset, $length, $replacement) {
// If the replacement isn't an array already then make it one
if (!is_array($replacement)) {
$replacement = array($replacement);
}
// Slice the array, then put the two parts back together with the replacement part at the desired spot
$out = array_slice($array, $offset, $length);
$array = array_slice($array, 0, $offset, true) + $replacement + array_slice($array, $offset + $length, NULL, true);
return $out;
}
?>