forked from oprimus/PHP-Zoho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZoho.php
133 lines (112 loc) · 4.26 KB
/
Zoho.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
<?php
include_once('ZohoCreator.php');
include_once('ZohoCrm.php');
include_once('ZohoReports.php');
abstract class Zoho {
public $scope;
public $authToken;
public $pathPrefix;
public $proxy;
public $fixedParams=array();
public $ticket=null;
public function __construct($scope, $pathPrefix, $authToken) {
$this->scope = $scope;
$this->pathPrefix = $pathPrefix;
$this->authToken = $authToken;
}
public function setProxy($proxy) {
$this->proxy = $proxy;
}
public function __destruct() {
$this->logout();
}
public function parseAccountAction($str) {
$result = array();
foreach (split("\r?\n", $str) as $line) {
if (substr(trim($line), 0, 1) == '#') continue;
if (preg_match('/([a-zA-Z]+)=(.+)/', trim($line), $m)) {
$result[$m[1]] = $m[2];
}
}
return $result;
}
public function login($username, $password) {
if ($this->ticket === null ) {
$postData = array(
'LOGIN_ID' => $username,
'PASSWORD' => $password,
'FROM_AGENT' => 'true',
'servicename' => 'ZohoReports'
);
$resultStr = $this->callUrl('https://accounts.zoho.com/login', array(), array('PostData' => $postData));
$result = $this->parseAccountAction($resultStr);
if (array_key_exists('RESULT', $result) && array_key_exists('TICKET', $result) && $result['RESULT'] == 'TRUE') {
$this->ticket = $result['TICKET'];
} else {
throw new Exception("Unable to login to Zoho: " . $resultStr);
}
}
return $this->ticket;
}
public function logout() {
if ($this->ticket !== null ) {
$postData = array(
'ticket' => $this->ticket,
'FROM_AGENT' => 'true'
);
$resultStr = $this->callUrl('https://accounts.zoho.com/logout', array(), array('PostData' => $postData));
$result = $this->parseAccountAction($resultStr);
if (array_key_exists('RESULT', $result) && $result['RESULT'] == 'TRUE') {
return true;
} else {
throw new Exception("Unable to logout of Zoho: " . $resultStr);
}
}
return false;
}
public function callXml($path, $params=array(), $options=array()) {
$params = $this->fixedParams+array('scope' => $this->scope, 'authtoken' => $this->authToken)+$params;
$url = $this->pathPrefix . '/xml/' . $path;
return $this->callUrl($url, $params, $options);
}
public function call($path, $params=array(), $options=array()) {
$params = $this->fixedParams+array('scope' => $this->scope, 'authtoken' => $this->authToken)+$params;
$url = $this->pathPrefix . '/json/' . $path;
return json_decode($this->callUrl($url, $params, $options));
}
public function callUrl($url, $params=array(), $options=array()) {
// Initialise CURL
$ch = curl_init();
if ($this->proxy) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// Post data
if (array_key_exists('PostData', $options) && is_array($options['PostData'])) {
curl_setopt($ch, CURLOPT_POST, true);
if (!empty($options['PostData'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $options['PostData']);
}
}
// Build URL
$paramparts = array();
foreach ($params as $key => $val) {
$paramparts[] = urlencode($key) . '=' . urlencode($val);
}
$url = $url;
if (!empty($paramparts)) {
$url .= '?' . implode('&', $paramparts);
}
curl_setopt($ch, CURLOPT_URL, $url);
// Call
$output = curl_exec($ch);
// Analyse result
if ($output === false) {
throw new Exception(sprintf("CURL error (#%d) \"%s\". Requested URL was \"%s\".", curl_errno($ch), curl_error($ch), $url));
}
curl_close($ch);
return $output;
}
}