5 * This class allows to consume the API, either from within a module that knows civicrm already:
8 * require_once('api/class/api.php');
9 * $api = new civicrm_api3();
12 * or from any code on the same server as civicrm
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));
20 * or to query a remote server via the rest api
23 * $api = new civicrm_api3 (array ('server' => 'http://example.org','api_key'=>'theusersecretkey','key'=>'thesitesecretkey'));
26 * No matter how initialised and if civicrm is local or remote, you use the class the same way.
29 * $api->{entity}->{action}($params);
32 * So, to get the individual contacts:
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;
41 * // in theory, doesn't append
43 * echo $api->errorMsg();
47 * Or, to create an event:
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;
53 * echo $api->errorMsg();
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
62 * $api->Activity->Get (42);
63 * $api->Activity->Get (array('id'=>42));
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']).
72 * $result = $api->result;
73 * // is the json encoded result
80 * @param array API configuration.
82 function __construct($config = NULL) {
84 $this->input
= array();
85 $this->lastResult
= array();
86 if (isset($config) && isset($config['server'])) {
87 // we are calling a remote server via REST
89 $this->uri
= $config['server'];
90 if (isset($config['path'])) {
91 $this->uri
.= "/" . $config['path'];
94 $this->uri
.= '/sites/all/modules/civicrm/extern/rest.php';
96 $this->uri
.= '?json=1';
97 if (isset($config['key'])) {
98 $this->key
= $config['key'];
101 die("\nFATAL:param['key] missing\n");
103 if (isset($config['api_key'])) {
104 $this->api_key
= $config['api_key'];
107 die("\nFATAL:param['api_key] missing\n");
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
;
114 require_once 'CRM/Core/Classloader.php';
115 require_once 'api/api.php';
116 require_once "api/v3/utils.php";
117 CRM_Core_Classloader
::singleton()->register();
118 $this->cfg
= CRM_Core_Config
::singleton();
122 $this->cfg
= CRM_Core_Config
::singleton();
129 public function __toString() {
130 return json_encode($this->lastResult
);
136 public function __call($action, $params) {
137 // @TODO Check if it's a valid action.
138 if (isset($params[0])) {
139 return $this->call($this->currentEntity
, $action, $params[0]);
142 return $this->call($this->currentEntity
, $action, $this->input
);
149 public static function __callStatic($name, $arguments) {
150 // Should we implement it ?
151 echo "Calling static method '$name' " . implode(', ', $arguments) . "\n";
157 function remoteCall($entity, $action, $params = array()) {
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);
163 if (function_exists('curl_init')) {
164 // To facilitate debugging without leaking info, entity & action
165 // are GET, other data is POST.
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);
171 $result = curl_exec($ch);
172 // CiviCRM expects to get back a CiviCRM error object.
173 if (curl_errno($ch)) {
176 $res->error
= array('cURL error' => curl_error($ch));
180 return json_decode($result);
183 // Should be discouraged, because the API credentials and data
184 // are submitted as GET data, increasing chance of exposure..
185 $result = file_get_contents($query . '&' . $fields);
186 return json_decode($result);
190 function call($entity, $action = 'Get', $params = array()) {
191 if (is_int($params)) {
192 $params = array('id' => $params);
194 elseif (is_string($params)) {
195 $params = json_decode($params);
198 if (!isset($params['version'])) {
199 $params['version'] = 3;
201 if (!isset($params['sequential'])) {
202 $params['sequential'] = 1;
206 $this->lastResult
= $this->remoteCall($entity, $action, $params);
209 // Converts a multi-dimentional array into an object.
210 $this->lastResult
= json_decode(json_encode(civicrm_api($entity, $action, $params)));
212 // Reset the input to be ready for a new call.
213 $this->input
= array();
214 if (property_exists($this->lastResult
, 'is_error')) {
215 return !$this->lastResult
->is_error
;
217 // getsingle doesn't have is_error.
222 * Helper method for long running programs (eg bots).
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");
237 * Return the last error message.
239 function errorMsg() {
240 return $this->lastResult
->error_message
;
247 CRM_Core_DAO
::init($this->cfg
->dsn
);
253 public function attr($name, $value = NULL) {
254 if ($value === NULL) {
255 if (property_exists($this->lastResult
, $name)) {
256 return $this->lastResult
->$name;
260 $this->input
[$name] = $value;
268 public function is_error() {
269 return (property_exists($this->lastResult
, 'is_error') && $this->lastResult
->is_error
);
275 public function is_set($name) {
276 return (isset($this->lastResult
->$name));
282 public function __get($name) {
283 // @TODO Test if valid entity.
284 if (strtolower($name) !== $name) {
285 // Cheap and dumb test to differentiate call to
286 // $api->Entity->Action & value retrieval.
287 $this->currentEntity
= $name;
290 if ($name === 'result') {
291 return $this->lastResult
;
293 if ($name === 'values') {
294 return $this->lastResult
->values
;
296 if (property_exists($this->lastResult
, $name)) {
297 return $this->lastResult
->$name;
299 $this->currentEntity
= $name;
304 * Or use $api->value.
306 public function values() {
307 if (is_array($this->lastResult
)) {
308 return $this->lastResult
['values'];
311 return $this->lastResult
->values
;
316 * Or use $api->result.
318 public function result() {
319 return $this->lastResult
;