civicrm_api3
in package
This class allows to consume the API, either from within a module that knows civicrm already:
require_once('api/class.api.php');
$api = new civicrm_api3();
or from any code on the same server as civicrm
require_once('/your/civi/folder/api/class.api.php');
// the path to civicrm.settings.php
$api = new civicrm_api3 (['conf_path'=> '/your/path/to/your/civicrm/or/joomla/site']);
or to query a remote server via the rest api
$api = new civicrm_api3 (['server' => 'http://example.org',
'api_key'=>'theusersecretkey',
'key'=>'thesitesecretkey']);
No matter how initialised and if civicrm is local or remote, you use the class the same way.
$api->{entity}->{action}($params);
So, to get the individual contacts:
if ($api->Contact->Get(['contact_type'=>'Individual','return'=>'sort_name,current_employer']) {
// each key of the result array is an attribute of the api
echo "\n contacts found " . $api->count;
foreach ($api->values as $c) {
echo "\n".$c->sort_name. " working for ". $c->current_employer;
}
// in theory, doesn't append
} else {
echo $api->errorMsg();
}
Or, to create an event:
if ($api->Event->Create(['title'=>'Test','event_type_id' => 1,'is_public' => 1,'start_date' => 19430429])) {
echo "created event id:". $api->id;
} else {
echo $api->errorMsg();
}
To make it easier, the Actions can either take for input an associative array $params, or simply an id. The following two lines are equivalent.
$api->Activity->Get (42);
$api->Activity->Get (['id'=>42]);
You can also get the result like civicrm_api does, but as an object instead of an array (eg $entity->attribute instead of $entity['attribute']).
$result = $api->result;
// is the json encoded result
echo $api;
For remote calls, you may need to set the UserAgent and Referer strings for some environments (eg WordFence) Add 'referer' and 'useragent' to the initialisation config:
$api = new civicrm_api3 (['server' => 'http://example.org',
'api_key'=>'theusersecretkey',
'key'=>'thesitesecretkey',
'referer'=>'https://my_site',
'useragent'=>'curl']);
Table of Contents
Properties
- $local : bool
- Are we performing a local or remote API call?
- $cfg : CRM_Core_Config
- Reference to the CRM_Core_Config singleton
- $currentEntity : string|null
- The current entity, which actions should be performed against
Methods
- __call() : bool
- Perform action.
- __construct() : mixed
- Class constructor.
- __get() : $this
- Get object.
- __toString() : string
- Convert to string.
- attr() : $this
- Get attribute.
- errorMsg() : string
- Return the last error message.
- init() : mixed
- Initialize.
- is_error() : bool
- Is this an error.
- is_set() : bool
- Check if var is set.
- ping() : mixed
- Helper method for long running programs (eg bots).
- result() : array<string|int, mixed>
- Or use $api->result.
- values() : array<string|int, mixed>
- Or use $api->value.
- call() : bool
- Call api function.
- remoteCall() : stdClass
- Call via rest.
Properties
$local
Are we performing a local or remote API call?
public
bool
$local
= \TRUE
$cfg
Reference to the CRM_Core_Config singleton
protected
CRM_Core_Config
$cfg
$currentEntity
The current entity, which actions should be performed against
protected
string|null
$currentEntity
= \NULL
Methods
__call()
Perform action.
public
__call(string $action, array<string|int, mixed> $params) : bool
Parameters
- $action : string
- $params : array<string|int, mixed>
Return values
bool__construct()
Class constructor.
public
__construct([array<string|int, mixed> $config = NULL ]) : mixed
Parameters
- $config : array<string|int, mixed> = NULL
-
API configuration.
__get()
Get object.
public
__get(string $name) : $this
Parameters
- $name : string
Return values
$this__toString()
Convert to string.
public
__toString() : string
Return values
stringattr()
Get attribute.
public
attr(string $name[, mixed $value = NULL ]) : $this
Parameters
- $name : string
- $value : mixed = NULL
Return values
$thiserrorMsg()
Return the last error message.
public
errorMsg() : string
Return values
stringinit()
Initialize.
public
init() : mixed
is_error()
Is this an error.
public
is_error() : bool
Return values
boolis_set()
Check if var is set.
public
is_set(string $name) : bool
Parameters
- $name : string
Return values
boolping()
Helper method for long running programs (eg bots).
public
ping() : mixed
result()
Or use $api->result.
public
result() : array<string|int, mixed>
Return values
array<string|int, mixed>values()
Or use $api->value.
public
values() : array<string|int, mixed>
Return values
array<string|int, mixed>call()
Call api function.
private
call(string $entity[, string $action = 'Get' ][, array<string|int, mixed> $params = [] ]) : bool
Parameters
- $entity : string
- $action : string = 'Get'
- $params : array<string|int, mixed> = []
Return values
boolremoteCall()
Call via rest.
private
remoteCall(string $entity, string $action[, array<string|int, mixed> $params = [] ]) : stdClass
Parameters
- $entity : string
- $action : string
- $params : array<string|int, mixed> = []