core 3459 getlist leading zero fix
[civicrm-core.git] / api / class.api.php
index df6ef1283f4c0c4149987fcc73cec06f868743b3..21a4190b70c6d523083edb0ad08f7d7b8ff439c1 100644 (file)
  *   // 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 {
 
@@ -109,6 +120,8 @@ class civicrm_api3 {
       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 (!empty($config) && !empty($config['conf_path'])) {
@@ -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
    */