X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=api%2Fclass.api.php;h=21a4190b70c6d523083edb0ad08f7d7b8ff439c1;hb=e859e64134e6e181ac45d9c027998918f4cd921a;hp=4ba61dd30bde6c5d50e8e25d839a895f07d596fb;hpb=2c20f50e843b464ce6c93c264f2545cff3e3edea;p=civicrm-core.git diff --git a/api/class.api.php b/api/class.api.php index 4ba61dd30b..21a4190b70 100644 --- a/api/class.api.php +++ b/api/class.api.php @@ -14,15 +14,15 @@ * ``` * require_once('/your/civi/folder/api/class.api.php'); * // the path to civicrm.settings.php - * $api = new civicrm_api3 (array('conf_path'=> '/your/path/to/your/civicrm/or/joomla/site)); + * $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 (array ('server' => 'http://example.org', - * 'api_key'=>'theusersecretkey', - * 'key'=>'thesitesecretkey')); + * $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. @@ -34,7 +34,7 @@ * So, to get the individual contacts: * * ``` - * if ($api->Contact->Get(array('contact_type'=>'Individual','return'=>'sort_name,current_employer')) { + * 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) { @@ -49,7 +49,7 @@ * Or, to create an event: * * ``` - * if ($api->Event->Create(array('title'=>'Test','event_type_id' => 1,'is_public' => 1,'start_date' => 19430429))) { + * 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(); @@ -62,7 +62,7 @@ * * ``` * $api->Activity->Get (42); - * $api->Activity->Get (array('id'=>42)); + * $api->Activity->Get (['id'=>42]); * ``` * * @@ -75,6 +75,17 @@ * // 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']); + * ``` */ class civicrm_api3 { @@ -87,31 +98,33 @@ class civicrm_api3 { $this->local = TRUE; $this->input = []; $this->lastResult = []; - if (isset($config) && isset($config['server'])) { + if (!empty($config) && !empty($config['server'])) { // we are calling a remote server via REST $this->local = FALSE; $this->uri = $config['server']; - if (isset($config['path'])) { + if (!empty($config['path'])) { $this->uri .= "/" . $config['path']; } else { $this->uri .= '/sites/all/modules/civicrm/extern/rest.php'; } - if (isset($config['key'])) { + if (!empty($config['key'])) { $this->key = $config['key']; } else { die("\nFATAL:param['key] missing\n"); } - if (isset($config['api_key'])) { + if (!empty($config['api_key'])) { $this->api_key = $config['api_key']; } else { die("\nFATAL:param['api_key] missing\n"); } + $this->referer = !empty($config['referer']) ? $config['referer'] : ''; + $this->useragent = !empty($config['useragent']) ? $config['useragent'] : 'curl'; return; } - if (isset($config) && isset($config['conf_path'])) { + if (!empty($config) && !empty($config['conf_path'])) { if (!defined('CIVICRM_SETTINGS_PATH')) { define('CIVICRM_SETTINGS_PATH', $config['conf_path'] . '/civicrm.settings.php'); } @@ -140,8 +153,8 @@ class civicrm_api3 { /** * Perform action. * - * @param $action - * @param $params + * @param string $action + * @param array $params * * @return bool */ @@ -158,8 +171,8 @@ class civicrm_api3 { /** * Call via rest. * - * @param $entity - * @param $action + * @param string $entity + * @param string $action * @param array $params * * @return \stdClass @@ -180,6 +193,10 @@ class civicrm_api3 { curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent); + if ($this->referer) { + curl_setopt($ch, CURLOPT_REFERER, $this->referer); + } $result = curl_exec($ch); // CiviCRM expects to get back a CiviCRM error object. if (curl_errno($ch)) { @@ -211,7 +228,7 @@ class civicrm_api3 { /** * Call api function. * - * @param $entity + * @param string $entity * @param string $action * @param array $params * @@ -281,8 +298,8 @@ class civicrm_api3 { /** * Get attribute. * - * @param $name - * @param null $value + * @param string $name + * @param mixed $value * * @return $this */