-
Notifications
You must be signed in to change notification settings - Fork 15
/
globals.php
229 lines (194 loc) · 5.67 KB
/
globals.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
227
228
229
<?php
/**
* Globals and utilities for OAuth Examaples package
*/
// Fill in the next two constants
define('OAUTH_CONSUMER_KEY', 'REPLACE_ME');
define('OAUTH_CONSUMER_SECRET', 'REPLACE_ME');
$progname = $argv[0];
$debug = 0; // Set to 1 for verbose debugging output
function logit($msg,$preamble=true)
{
// If you get timezone warnings from PHP, then
// fill in the correct timezone, and uncomment the next line.
//date_default_timezone_set('America/Los_Angeles');
$now = date(DateTime::ISO8601, time());
error_log(($preamble ? "+++${now}:" : '') . $msg);
}
/**
* Do an HTTP GET
* @param string $url
* @param int $port (optional)
* @param array $headers an array of HTTP headers (optional)
* @return array ($info, $header, $response) on success or empty array on error.
*/
function do_get($url, $port=80, $headers=NULL)
{
$retarr = array(); // Return value
$curl_opts = array(CURLOPT_URL => $url,
CURLOPT_PORT => $port,
CURLOPT_POST => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true);
if ($headers) { $curl_opts[CURLOPT_HTTPHEADER] = $headers; }
$response = do_curl($curl_opts);
if (! empty($response)) { $retarr = $response; }
return $retarr;
}
/**
* Do an HTTP POST
* @param string $url
* @param int $port (optional)
* @param array $headers an array of HTTP headers (optional)
* @return array ($info, $header, $response) on success or empty array on error.
*/
function do_post($url, $postbody, $port=80, $headers=NULL)
{
$retarr = array(); // Return value
$curl_opts = array(CURLOPT_URL => $url,
CURLOPT_PORT => $port,
CURLOPT_POST => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => $postbody,
CURLOPT_RETURNTRANSFER => true);
if ($headers) { $curl_opts[CURLOPT_HTTPHEADER] = $headers; }
$response = do_curl($curl_opts);
if (! empty($response)) { $retarr = $response; }
return $retarr;
}
/**
* Make a curl call with given options.
* @param array $curl_opts an array of options to curl
* @return array ($info, $header, $response) on success or empty array on error.
*/
function do_curl($curl_opts)
{
global $debug;
$retarr = array(); // Return value
if (! $curl_opts) {
if ($debug) { logit("do_curl:ERR:curl_opts is empty"); }
return $retarr;
}
// Open curl session
$ch = curl_init();
if (! $ch) {
if ($debug) { logit("do_curl:ERR:curl_init failed"); }
return $retarr;
}
// Set curl options that were passed in
curl_setopt_array($ch, $curl_opts);
// Ensure that we receive full header
curl_setopt($ch, CURLOPT_HEADER, true);
if ($debug) {
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
}
// Send the request and get the response
ob_start();
$response = curl_exec($ch);
$curl_spew = ob_get_contents();
ob_end_clean();
if ($debug && $curl_spew) {
logit("do_curl:INFO:curl_spew begin");
logit($curl_spew, false);
logit("do_curl:INFO:curl_spew end");
}
// Check for errors
if (curl_errno($ch)) {
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
if ($debug) { logit("do_curl:ERR:$errno:$errmsg"); }
curl_close($ch);
unset($ch);
return $retarr;
}
if ($debug) {
logit("do_curl:DBG:header sent begin");
$header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
logit($header_sent, false);
logit("do_curl:DBG:header sent end");
}
// Get information about the transfer
$info = curl_getinfo($ch);
// Parse out header and body
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size );
// Close curl session
curl_close($ch);
unset($ch);
if ($debug) {
logit("do_curl:DBG:response received begin");
if (!empty($response)) { logit($response, false); }
logit("do_curl:DBG:response received end");
}
// Set return value
array_push($retarr, $info, $header, $body);
return $retarr;
}
/**
* Pretty print some JSON
* @param string $json The packed JSON as a string
* @param bool $html_output true if the output should be escaped
* (for use in HTML)
* @link http://us2.php.net/manual/en/function.json-encode.php#80339
*/
function json_pretty_print($json, $html_output=false)
{
$spacer = ' ';
$level = 1;
$indent = 0; // current indentation level
$pretty_json = '';
$in_string = false;
$len = strlen($json);
for ($c = 0; $c < $len; $c++) {
$char = $json[$c];
switch ($char) {
case '{':
case '[':
if (!$in_string) {
$indent += $level;
$pretty_json .= $char . "\n" . str_repeat($spacer, $indent);
} else {
$pretty_json .= $char;
}
break;
case '}':
case ']':
if (!$in_string) {
$indent -= $level;
$pretty_json .= "\n" . str_repeat($spacer, $indent) . $char;
} else {
$pretty_json .= $char;
}
break;
case ',':
if (!$in_string) {
$pretty_json .= ",\n" . str_repeat($spacer, $indent);
} else {
$pretty_json .= $char;
}
break;
case ':':
if (!$in_string) {
$pretty_json .= ": ";
} else {
$pretty_json .= $char;
}
break;
case '"':
if ($c > 0 && $json[$c-1] != '\\') {
$in_string = !$in_string;
}
default:
$pretty_json .= $char;
break;
}
}
return ($html_output) ?
'<pre>' . htmlentities($pretty_json) . '</pre>' :
$pretty_json . "\n";
}
?>