Answers.Body: <p>Call <code>civicrm_initialize()</code> before using <code>civicrm_api3()</code> (or other CiviCRM functions).</p>
  
  <p>This function is defined in the various CMS plugins (eg <code>civicrm.module</code>) and will load required files then initialize CiviCRM, after which you can make API calls.</p>
  
  <p>Since booting the CRM adds overhead, it's best to only do when CRM is required - hence the separate function.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-11T16:36:31.210Z
Answers.Text: Civicrm_initialize() generates a fatal error call to undefined function civicrm_initialize()
Answers.UserDisplayName: ermSO
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-11T16:46:44.107Z
Answers.Text: Can you paste the url you trying to call on ajax?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-11T01:07:23.473Z
Answers.LastActivityDate: 2018-01-11T04:04:01.350Z
Answers.LastEditDate: 2018-01-11T04:04:01.350Z
Answers.UserDisplayName: Pradeep Nayak
Answers.Body: <p>I used the following.  It is important to note that my current working directory was that of the website not the plugin directory, so when I did the require_once and conf_path, I had to take that into account for class.api.php and civicrm.settings.php (i.e. my path was like administrator/.../class.api.php)</p>
  
  <pre><code>require_once 'your/civicrm/folder/api/class.api.php';
  $api = new civicrm_api3(array(
    // Specify location of "civicrm.settings.php".
    'conf_path' =&gt; 'your/sites/default',
  ));
  $apiParams = array(
    'first_name' =&gt; 'Alice',
    'last_name' =&gt; 'Roberts',
  );
  if ($api-&gt;Contact-&gt;Get($apiParams)) {
    //each key of the result array is an attribute of the api
    echo "\n contacts found ".$api-&gt;count;
  }
  else {
    echo $api-&gt;errorMsg();
  }
  </code></pre>
  
  <p>It really should be documented somewhere that you cannot bootstrap CiviCRM in an onAjax[Name] method.  In my case, when I tried it, the submit button for the form lost its functionality.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-11T16:48:50.717Z
Answers.LastActivityDate: 2018-01-11T16:48:50.717Z
Answers.UserDisplayName: ermSO
Body: <p>I created a plugin which provides custom validation for civicrm forms.  Part of the jQuery validation on the plugin makes an AJAX call to the plugin.  The plugin calls its onAjax[Name] method.  In the onAjax[Name] method, I want to use civicrm_api3( ... ).  However, the system returns a fatal error</p>
  
  <p>Call to undefined function civicrm_api3()</p>
  
  <p>How do I use civicrm_api3 in the above context?  Do I need to import some class files?  The same code works if the validation occurs when the form is submitted with the submit button instead of AJAX.</p>
  
Tags: <api><joomla><php><ajax><plugin>
Title: Using Civicrm_api3 in onAjax[Name] plugin function
ThreadText: # Using Civicrm_api3 in onAjax[Name] plugin function
  
  (Asked by: username_0 on 2018-01-10)
  
  I created a plugin which provides custom validation for civicrm forms. Part of the jQuery validation on the plugin makes an AJAX call to the plugin. The plugin calls its onAjax[Name] method. In the onAjax[Name] method, I want to use civicrm\_api3( ... ). However, the system returns a fatal error
  
  Call to undefined function civicrm\_api3()
  
  How do I use civicrm\_api3 in the above context? Do I need to import some class files? The same code works if the validation occurs when the form is submitted with the submit button instead of AJAX.
  
  **Tags:** api, joomla, php, ajax, plugin
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-01-11. Score: 2
  
  Call `civicrm_initialize()` before using `civicrm_api3()` (or other CiviCRM functions).
  
  This function is defined in the various CMS plugins (eg `civicrm.module`) and will load required files then initialize CiviCRM, after which you can make API calls.
  
  Since booting the CRM adds overhead, it's best to only do when CRM is required - hence the separate function.
  
  > **Comments:**
  > **username_0**: Civicrm_initialize() generates a fatal error call to undefined function civicrm_initialize()  
  > **username_1**: Can you paste the url you trying to call on ajax?  
  
  ---
  
  ### Answer by username_0 on 2018-01-11. Score: 0
  
  I used the following.  It is important to note that my current working directory was that of the website not the plugin directory, so when I did the require_once and conf_path, I had to take that into account for class.api.php and civicrm.settings.php (i.e. my path was like administrator/.../class.api.php)
  require_once 'your/civicrm/folder/api/class.api.php';
  $api = new civicrm_api3(array(
    // Specify location of "civicrm.settings.php".
    'conf_path' => 'your/sites/default',
  ));
  $apiParams = array(
    'first_name' => 'Alice',
    'last_name' => 'Roberts',
  );
  if ($api->Contact->Get($apiParams)) {
    //each key of the result array is an attribute of the api
    echo "\n contacts found ".$api->count;
  }
  else {
    echo $api->errorMsg();
  }
  
  It really should be documented somewhere that you cannot bootstrap CiviCRM in an onAjax[Name] method.  In my case, when I tried it, the submit button for the form lost its functionality.
  
  
  ---
  
  