-
Notifications
You must be signed in to change notification settings - Fork 1
/
webform_salsa.apis.inc~
81 lines (60 loc) · 2.35 KB
/
webform_salsa.apis.inc~
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
<?php
/**
* @file
* API for Salsa webform integration
*/
class salsaAPI {
public $email = 'PASS_EMAIL_WHEN_INSTANTIATING_CLASS';
public $passwd = 'PASS_PASSWORD_WHEN_INSTANTIATING_CLASS';
public $nodeURL = 'PASS_NODE_URL_WHEN_INSTANTIATING_CLASS';
public function __construct($nodeURL = null, $email = null, $passwd = null) {
if(!extension_loaded('curl')) trigger_error('salsaAPI requires PHP cURL', E_USER_ERROR);
if(is_null($email)) trigger_error('email must be supplied', E_USER_ERROR);
if(is_null($passwd)) trigger_error('password must be supplied', E_USER_ERROR);
if(is_null($nodeURL)) trigger_error('Salsa node URL must be supplied', E_USER_ERROR);
$this->email = $email;
$this->passwd = $passwd;
$this->nodeURL = $nodeURL;
}
/* general API call functions */
public function authenticate() {
$ch = curl_init();
$authfields["email"] = $this->email;
$authfields["password"] = $this->passwd;
$url = $this->nodeURL;
// Set basic connection parameters:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
// Set Parameters to maintain cookies across sessions
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies_file');
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies_file');
curl_setopt($ch, CURLOPT_URL, "$url/api/authenticate.sjs");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($authfields));
$auth = curl_exec($ch);
curl_close($ch);
return $auth;
}
public function call($method, $fields, $returnraw = false) {
$ch = curl_init();
$url = $this->nodeURL;
// Set basic connection parameters:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
// Set Parameters to maintain cookies across sessions
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies_file');
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies_file');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_URL, "$url/$method");
$response = curl_exec($ch);
curl_close($ch);
return $returnraw ? $reponse : $this->response_to_xml($response);
}
private function response_to_xml($response) {
$xml = simplexml_load_string($response);
return $xml;
}
}