-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.php
More file actions
66 lines (59 loc) · 1.67 KB
/
Copy pathutil.php
File metadata and controls
66 lines (59 loc) · 1.67 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
<?php
//Modified from http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
function rest_helper($url, $params = null, $verb = 'GET', $format = 'json')
{
$cparams = array(
'http' => array(
'method' => $verb,
'ignore_errors' => true
)
);
if ($params['body'] !== null) {
$data = $params['body'];
if ($verb != 'GET') {
$cparams['http']['content'] = $data;
} else {
$url .= '?' . $data;
}
}
$cparams['http']['header'] = 'User-Agent: Git-Syllabus (https://github.com/emac-utd/git-syllabus)\r\n';
if ($params['headers'] !== null) {
foreach($params['headers'] as $header => $value)
{
$cparams['http']['header'] .= $header . ': ' . $value . "\r\n";
}
}
$context = stream_context_create($cparams);
$fp = fopen($url, 'rb', false, $context);
if (!$fp) {
$res = false;
} else {
// If you're trying to troubleshoot problems, try uncommenting the
// next two lines; it will show you the HTTP response headers across
// all the redirects:
//$meta = stream_get_meta_data($fp);
//error_log(print_r($cparams, true));
//error_log(print_r($meta['wrapper_data'], true));
$res = stream_get_contents($fp);
//error_log($res);
}
if ($res === false) {
throw new Exception("$verb $url failed: $php_errormsg");
}
switch ($format) {
case 'json':
$r = json_decode($res);
if ($r === null) {
throw new Exception("failed to decode $res as json");
}
return $r;
case 'xml':
$r = simplexml_load_string($res);
if ($r === null) {
throw new Exception("failed to decode $res as xml");
}
return $r;
}
return $res;
}
?>