Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /** | |
ade83e4b CB |
4 | * |
5 | * This class allows to consume the API, either from within a module that knows civicrm already: | |
6 | * | |
7 | * @code | |
0cd417ef | 8 | * require_once('api/class.api.php'); |
ade83e4b CB |
9 | * $api = new civicrm_api3(); |
10 | * @endcode | |
11 | * | |
12 | * or from any code on the same server as civicrm | |
13 | * | |
14 | * @code | |
15 | * require_once('/your/civi/folder/api/class.api.php'); | |
16 | * // the path to civicrm.settings.php | |
17 | * $api = new civicrm_api3 (array('conf_path'=> '/your/path/to/your/civicrm/or/joomla/site)); | |
18 | * @endcode | |
19 | * | |
20 | * or to query a remote server via the rest api | |
21 | * | |
22 | * @code | |
0cd417ef KW |
23 | * $api = new civicrm_api3 (array ('server' => 'http://example.org', |
24 | * 'api_key'=>'theusersecretkey', | |
25 | * 'key'=>'thesitesecretkey')); | |
ade83e4b CB |
26 | * @endcode |
27 | * | |
28 | * No matter how initialised and if civicrm is local or remote, you use the class the same way. | |
29 | * | |
30 | * @code | |
31 | * $api->{entity}->{action}($params); | |
32 | * @endcode | |
33 | * | |
34 | * So, to get the individual contacts: | |
35 | * | |
36 | * @code | |
37 | * if ($api->Contact->Get(array('contact_type'=>'Individual','return'=>'sort_name,current_employer')) { | |
38 | * // each key of the result array is an attribute of the api | |
39 | * echo "\n contacts found " . $api->count; | |
40 | * foreach ($api->values as $c) { | |
41 | * echo "\n".$c->sort_name. " working for ". $c->current_employer; | |
42 | * } | |
43 | * // in theory, doesn't append | |
44 | * } else { | |
45 | * echo $api->errorMsg(); | |
46 | * } | |
47 | * @endcode | |
48 | * | |
49 | * Or, to create an event: | |
50 | * | |
51 | * @code | |
52 | * if ($api->Event->Create(array('title'=>'Test','event_type_id' => 1,'is_public' => 1,'start_date' => 19430429))) { | |
53 | * echo "created event id:". $api->id; | |
54 | * } else { | |
55 | * echo $api->errorMsg(); | |
56 | * } | |
57 | * @endcode | |
58 | * | |
59 | * To make it easier, the Actions can either take for input an | |
60 | * associative array $params, or simply an id. The following two lines | |
61 | * are equivalent. | |
62 | * | |
63 | * @code | |
64 | * $api->Activity->Get (42); | |
65 | * $api->Activity->Get (array('id'=>42)); | |
66 | * @endcode | |
67 | * | |
68 | * | |
69 | * You can also get the result like civicrm_api does, but as an object | |
70 | * instead of an array (eg $entity->attribute instead of | |
71 | * $entity['attribute']). | |
72 | * | |
73 | * @code | |
74 | * $result = $api->result; | |
75 | * // is the json encoded result | |
76 | * echo $api; | |
77 | * @endcode | |
6a488035 TO |
78 | */ |
79 | class civicrm_api3 { | |
ade83e4b CB |
80 | |
81 | /** | |
82 | * @param array API configuration. | |
83 | */ | |
6a488035 TO |
84 | function __construct($config = NULL) { |
85 | $this->local = TRUE; | |
86 | $this->input = array(); | |
87 | $this->lastResult = array(); | |
88 | if (isset($config) && isset($config['server'])) { | |
89 | // we are calling a remote server via REST | |
90 | $this->local = FALSE; | |
91 | $this->uri = $config['server']; | |
92 | if (isset($config['path'])) { | |
93 | $this->uri .= "/" . $config['path']; | |
94 | } | |
ade83e4b CB |
95 | else { |
96 | $this->uri .= '/sites/all/modules/civicrm/extern/rest.php'; | |
97 | } | |
6a488035 TO |
98 | $this->uri .= '?json=1'; |
99 | if (isset($config['key'])) { | |
100 | $this->key = $config['key']; | |
101 | } | |
102 | else { | |
103 | die("\nFATAL:param['key] missing\n"); | |
104 | } | |
105 | if (isset($config['api_key'])) { | |
106 | $this->api_key = $config['api_key']; | |
107 | } | |
108 | else { | |
109 | die("\nFATAL:param['api_key] missing\n"); | |
110 | } | |
6a488035 TO |
111 | return; |
112 | } | |
113 | if (isset($config) && isset($config['conf_path'])) { | |
114 | define('CIVICRM_SETTINGS_PATH', $config['conf_path'] . '/civicrm.settings.php'); | |
115 | require_once CIVICRM_SETTINGS_PATH; | |
f9c7ce43 | 116 | require_once 'CRM/Core/ClassLoader.php'; |
6a488035 TO |
117 | require_once 'api/api.php'; |
118 | require_once "api/v3/utils.php"; | |
f9c7ce43 | 119 | CRM_Core_ClassLoader::singleton()->register(); |
6a488035 TO |
120 | $this->cfg = CRM_Core_Config::singleton(); |
121 | $this->init(); | |
122 | } | |
123 | else { | |
124 | $this->cfg = CRM_Core_Config::singleton(); | |
125 | } | |
126 | } | |
127 | ||
ade83e4b CB |
128 | /** |
129 | * | |
130 | */ | |
6a488035 TO |
131 | public function __toString() { |
132 | return json_encode($this->lastResult); | |
133 | } | |
134 | ||
ade83e4b CB |
135 | /** |
136 | * | |
137 | */ | |
6a488035 | 138 | public function __call($action, $params) { |
ade83e4b | 139 | // @TODO Check if it's a valid action. |
6a488035 TO |
140 | if (isset($params[0])) { |
141 | return $this->call($this->currentEntity, $action, $params[0]); | |
142 | } | |
143 | else { | |
144 | return $this->call($this->currentEntity, $action, $this->input); | |
145 | } | |
146 | } | |
147 | ||
ade83e4b CB |
148 | /** |
149 | * As of PHP 5.3.0 | |
150 | */ | |
6a488035 TO |
151 | public static function __callStatic($name, $arguments) { |
152 | // Should we implement it ? | |
153 | echo "Calling static method '$name' " . implode(', ', $arguments) . "\n"; | |
154 | } | |
155 | ||
ade83e4b CB |
156 | /** |
157 | * | |
158 | */ | |
159 | function remoteCall($entity, $action, $params = array()) { | |
6a488035 TO |
160 | $fields = "key={$this->key}&api_key={$this->api_key}"; |
161 | $query = $this->uri . "&entity=$entity&action=$action"; | |
162 | foreach ($params as $k => $v) { | |
163 | $fields .= "&$k=" . urlencode($v); | |
164 | } | |
165 | if (function_exists('curl_init')) { | |
ade83e4b CB |
166 | // To facilitate debugging without leaking info, entity & action |
167 | // are GET, other data is POST. | |
6a488035 TO |
168 | $ch = curl_init(); |
169 | curl_setopt($ch, CURLOPT_URL, $query); | |
170 | curl_setopt($ch, CURLOPT_POST, count($params) + 2); | |
171 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); | |
172 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
6a488035 | 173 | $result = curl_exec($ch); |
7a514fb1 CB |
174 | // CiviCRM expects to get back a CiviCRM error object. |
175 | if (curl_errno($ch)) { | |
ade83e4b CB |
176 | $res = new stdClass; |
177 | $res->is_error = 1; | |
7922c1d3 XD |
178 | $res->error_message = curl_error($ch); |
179 | $res->level = "cURL"; | |
ade83e4b CB |
180 | $res->error = array('cURL error' => curl_error($ch)); |
181 | return $res; | |
8ef12e64 | 182 | } |
6a488035 | 183 | curl_close($ch); |
6a488035 TO |
184 | } |
185 | else { | |
ade83e4b CB |
186 | // Should be discouraged, because the API credentials and data |
187 | // are submitted as GET data, increasing chance of exposure.. | |
6a488035 | 188 | $result = file_get_contents($query . '&' . $fields); |
6a488035 | 189 | } |
78a069a9 CB |
190 | if (!$res = json_decode($result)) { |
191 | $res = new stdClass; | |
192 | $res->is_error = 1; | |
193 | $res->error_message = 'Unable to parse returned JSON'; | |
194 | $res->level = 'json_decode'; | |
195 | $res->error = array('Unable to parse returned JSON' => $result); | |
196 | $res->row_result = $result; | |
197 | } | |
198 | return $res; | |
6a488035 TO |
199 | } |
200 | ||
aa1b1481 EM |
201 | /** |
202 | * @param $entity | |
203 | * @param string $action | |
204 | * @param array $params | |
205 | * | |
206 | * @return bool | |
207 | */ | |
ade83e4b | 208 | function call($entity, $action = 'Get', $params = array()) { |
6a488035 TO |
209 | if (is_int($params)) { |
210 | $params = array('id' => $params); | |
211 | } | |
212 | elseif (is_string($params)) { | |
213 | $params = json_decode($params); | |
214 | } | |
215 | ||
216 | if (!isset($params['version'])) { | |
6a488035 TO |
217 | $params['version'] = 3; |
218 | } | |
219 | if (!isset($params['sequential'])) { | |
220 | $params['sequential'] = 1; | |
221 | } | |
222 | ||
223 | if (!$this->local) { | |
224 | $this->lastResult = $this->remoteCall($entity, $action, $params); | |
225 | } | |
226 | else { | |
ade83e4b | 227 | // Converts a multi-dimentional array into an object. |
6a488035 TO |
228 | $this->lastResult = json_decode(json_encode(civicrm_api($entity, $action, $params))); |
229 | } | |
ade83e4b | 230 | // Reset the input to be ready for a new call. |
6a488035 TO |
231 | $this->input = array(); |
232 | if (property_exists($this->lastResult, 'is_error')) { | |
233 | return !$this->lastResult->is_error; | |
234 | } | |
ade83e4b | 235 | // getsingle doesn't have is_error. |
6a488035 TO |
236 | return TRUE; |
237 | } | |
238 | ||
ade83e4b CB |
239 | /** |
240 | * Helper method for long running programs (eg bots). | |
241 | */ | |
6a488035 TO |
242 | function ping() { |
243 | global $_DB_DATAOBJECT; | |
244 | foreach ($_DB_DATAOBJECT['CONNECTIONS'] as & $c) { | |
245 | if (!$c->connection->ping()) { | |
246 | $c->connect($this->cfg->dsn); | |
247 | if (!$c->connection->ping()) { | |
248 | die("we couldn't connect"); | |
249 | } | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
ade83e4b CB |
254 | /** |
255 | * Return the last error message. | |
256 | */ | |
6a488035 TO |
257 | function errorMsg() { |
258 | return $this->lastResult->error_message; | |
259 | } | |
260 | ||
ade83e4b CB |
261 | /** |
262 | * | |
263 | */ | |
6a488035 TO |
264 | function init() { |
265 | CRM_Core_DAO::init($this->cfg->dsn); | |
266 | } | |
267 | ||
ade83e4b CB |
268 | /** |
269 | * | |
6a488035 | 270 | */ |
6a488035 TO |
271 | public function attr($name, $value = NULL) { |
272 | if ($value === NULL) { | |
273 | if (property_exists($this->lastResult, $name)) { | |
274 | return $this->lastResult->$name; | |
275 | } | |
276 | } | |
277 | else { | |
278 | $this->input[$name] = $value; | |
279 | } | |
280 | return $this; | |
281 | } | |
282 | ||
ade83e4b CB |
283 | /** |
284 | * | |
285 | */ | |
6a488035 TO |
286 | public function is_error() { |
287 | return (property_exists($this->lastResult, 'is_error') && $this->lastResult->is_error); | |
288 | } | |
289 | ||
ade83e4b CB |
290 | /** |
291 | * | |
292 | */ | |
6a488035 TO |
293 | public function is_set($name) { |
294 | return (isset($this->lastResult->$name)); | |
295 | } | |
296 | ||
ade83e4b CB |
297 | /** |
298 | * | |
299 | */ | |
6a488035 | 300 | public function __get($name) { |
ade83e4b | 301 | // @TODO Test if valid entity. |
6a488035 | 302 | if (strtolower($name) !== $name) { |
ade83e4b CB |
303 | // Cheap and dumb test to differentiate call to |
304 | // $api->Entity->Action & value retrieval. | |
6a488035 TO |
305 | $this->currentEntity = $name; |
306 | return $this; | |
307 | } | |
6a488035 | 308 | if ($name === 'result') { |
6a488035 TO |
309 | return $this->lastResult; |
310 | } | |
311 | if ($name === 'values') { | |
312 | return $this->lastResult->values; | |
313 | } | |
6a488035 | 314 | if (property_exists($this->lastResult, $name)) { |
6a488035 TO |
315 | return $this->lastResult->$name; |
316 | } | |
6a488035 TO |
317 | $this->currentEntity = $name; |
318 | return $this; | |
319 | } | |
320 | ||
ade83e4b CB |
321 | /** |
322 | * Or use $api->value. | |
323 | */ | |
6a488035 TO |
324 | public function values() { |
325 | if (is_array($this->lastResult)) { | |
326 | return $this->lastResult['values']; | |
327 | } | |
ade83e4b CB |
328 | else { |
329 | return $this->lastResult->values; | |
330 | } | |
6a488035 TO |
331 | } |
332 | ||
ade83e4b CB |
333 | /** |
334 | * Or use $api->result. | |
335 | */ | |
6a488035 TO |
336 | public function result() { |
337 | return $this->lastResult; | |
338 | } | |
339 | } |