-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vtiger_Mobile_Curl_Class.php
75 lines (65 loc) · 2.3 KB
/
Vtiger_Mobile_Curl_Class.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
<?php
/***********************************************************************************
* Vtiger Extension for mobile webservice access using curl
* Version: 1.0
************************************************************************************/
class Vtiger_Mobile_Curl_Class {
var $endpoint_url;
var $user_id;
var $user_name; // login
var $user_key; // password
var $token;
var $curl_handler;
var $defaults = array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false, // ssl fix
CURLOPT_SSL_VERIFYHOST => false // ssl fix
);
//constructor saves the values
function __construct($url, $name, $key) {
$this->endpoint_url = $url;
$this->user_id = 0;
$this->user_name = $name;
$this->user_key = $key;
$this->token = 0;
}
function login() {
$curl_handler = curl_init();
$params = array("_operation" => "login", "username" => $this->user_name, "password" => $this->user_key);
$options = array(CURLOPT_URL => $this->endpointUrl, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => http_build_query($params));
curl_setopt_array($curl_handler, ($this->defaults + $options));
$result = curl_exec($curl_handler);
if (!$result) {
die(curl_error($curl_handler));
}
$jsonResponse = json_decode($result, true);
if ($jsonResponse["success"] == false) {
die("Login failed: ".$jsonResponse["error"]["message"]);
}
$this->user_id = $jsonResponse["result"]["login"]["userid"];
$sessionId = $jsonResponse["result"]["login"]["session"];
//save session id
$this->token = $sessionId;
return true;
}
function query($query, $module) {
$curl_handler = curl_init();
$params = array("_operation" => "query", "_session" => $this->token, "query" => $query, "module" => $module);
$options = array(CURLOPT_URL => $this->endpointUrl, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => http_build_query($params));
curl_setopt_array($curl_handler, ($this->defaults + $options));
$result = curl_exec($curl_handler);
if (!$result) {
die(curl_error($curl_handler));
}
$jsonResponse = json_decode($result, true);
if($jsonResponse["success"] == false) {
die("Query failed: ".$jsonResponse["error"]["message"]);
}
//Array of retrieved objects
$retrievedObjects = $jsonResponse["result"]["records"];
return $retrievedObjects;
}
}
?>