forked from robertmarsal/moodle-videoconverter-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_caller.php
63 lines (49 loc) · 1.79 KB
/
api_caller.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
<?php
/**
* @author Robert Boloc <[email protected]>
* @copyright 2014 Servei de Recursos Educatius (http://www.sre.urv.cat)
*/
class api_caller {
protected $token;
protected $config;
public function __construct($token, $config) {
$this->token = $token;
$this->config = $config;
}
/**
* Curl handler is not reused because it does not
* work well when mixing GET/POST requests. Because
* the use is not intensive we can regenerate the
* handle on each request without a major performance
* impact.
*/
public function get($request, $params = array()) {
$params['request'] = $request;
$params['token'] = $this->token;
$request_url = $this->config['api_entry'] . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
return (object) json_decode($response);
}
/**
* Curl handler is not reused because it does not
* work well when mixing GET/POST requests. Because
* the use is not intensive we can regenerate the
* handle on each request without a major performance
* impact.
*/
public function post($request, $params = array()) {
$params['request'] = $request;
$params['token'] = $this->token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->config['api_entry']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
return (object) json_decode($response);
}
}