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