-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerticalResponse.class.php
180 lines (162 loc) · 3.73 KB
/
VerticalResponse.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* VerticalResponse API v1 client library
*
* @author Voltroid <[email protected]>
*
*/
class VerticalResponse
{
private $api_key;
private $code;
private $api_url = 'https://vrapi.verticalresponse.com/api/v1';
private $timeout = 8;
public $http_status;
/**
* X-Domain header value if empty header will be not provided
* @var string|null
*/
private $enterprise_domain = null;
/**
* X-APP-ID header value if empty header will be not provided
* @var string|null
*/
private $app_id = null;
/**
* Set api key and optionally API endpoint
* @param $api_key
* @param null $api_url
*/
public function __construct($api_key, $sec, $code = null)
{
$this->api_key = $api_key;
$this->sec = $sec;
if (!empty($code)) {
$this->code = $code;
}
}
/**
* We can modify internal settings
* @param $key
* @param $value
*/
function __set($key, $value)
{
$this->{$key} = $value;
}
/**
* Get Access Token Validity
*
* @return mixed
*/
public function getAccessToken()
{
$resp = wp_remote_get("http".(is_ssl()?'s':'').":https://vrapi.verticalresponse.com/api/v1/oauth/access_token?client_id=".$this->api_key."&client_secret=".$this->sec."&redirect_uri=".OPPOINT_PLUGIN_URL."service/vr/redirect.php&code=".$this->code);
$body = json_decode(strstr($resp['body'],'{"'),true);
if(isset($body["error"]))
return array('status'=>0);
else
return array('status'=>1,'code'=>$body);
}
/**
* get Contact Lists
*
* @return mixed
*/
public function getLists()
{
return $this->call('lists');
}
/**
* get Custom Fields
*
* @return mixed
*/
public function getCustomFields()
{
return $this->call('custom_fields');
}
/**
* post Contact
*
* @return mixed
*/
public function addContact($data,$list)
{
return $this->call('lists/'.$list.'/contacts','POST',$data);
}
/**
* post Contact
*
* @return mixed
*/
public function getContact($data)
{
return $this->call('contacts','GET',$data);
}
/**
* Curl run request
*
* @param null $api_method
* @param string $http_method
* @param array $params
* @return mixed
* @throws Exception
*/
private function call($api_method = null, $http_method = 'GET', $params = array())
{
if (empty($api_method)) {
return (object)array(
'httpStatus' => '400',
'code' => '1010',
'codeDescription' => 'Error in external resources',
'message' => 'Invalid api method'
);
}
if($params && $http_method == 'GET')
$gp = http_build_query($params);
$params = json_encode($params);
$url = $this->api_url . '/' . $api_method.(isset($gp)? '?' . $gp : '');
$options = array(
CURLOPT_URL => $url,
CURLOPT_ENCODING => 'gzip,deflate',
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_HEADER => false,
CURLOPT_USERAGENT => 'PHP VerticalResponse client 0.0.1',
CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $this->code, 'Content-Type: application/json')
);
if(strlen(ini_get('curl.cainfo')) === 0) {
$options[CURLOPT_CAINFO] = dirname(__FILE__).'/cacert.pem';
}
if ($http_method == 'POST') {
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $params;
} else if ($http_method == 'DELETE') {
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
}
$curl = curl_init();
curl_setopt_array($curl, $options);
$response=array();
$response['data'] = json_decode(curl_exec($curl),true);
$response['http_status'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return (object)$response;
}
/**
* @param array $params
*
* @return string
*/
private function setParams($params = array())
{
$result = array();
if (is_array($params)) {
foreach ($params as $key => $value) {
$result[$key] = $value;
}
}
return http_build_query($result);
}
}