Answers.Body: <p>If anyone else has this issue, it turns out that the CiviCRM module is not always loaded for not logged in users. Manually including the CRM API file fixed the problem:</p>
  
  <pre><code>    define('DRUPAL_ROOT', '/buildkit/build/CiviCRM');
      define('CIVI_ROOT', DRUPAL_ROOT . '/sites/all/modules/civicrm');
      define('API_PATH', CIVI_ROOT . '/api/api.php');
  
      /* Bootstrap into the Civi environment */
      require_once API_PATH;
      require_once CIVI_ROOT . '/civicrm.config.php';
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-26T10:33:57.073Z
Answers.Text: you sould also be able to call civicrm_initialize() which will do the same sort of job
Answers.UserDisplayName: Seamus Lee
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-26T01:27:15.977Z
Answers.LastActivityDate: 2017-05-26T01:27:15.977Z
Answers.UserDisplayName: DMCoding
Answers.Body: <p>The recommended way to include the necessary files to run the php API is to call civicrm_initialize() before the API call.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-06T15:42:04.350Z
Answers.LastActivityDate: 2017-07-06T15:42:04.350Z
Answers.UserDisplayName: Bryan D.
Body: <p>I'm having a strange problem using the CRM API thru PhP (i.e. not REST or AJAX). I'm trying to build a donations counter which tracks the total amount of contributions made under a specific contribution source. Everything works when I'm logged in. But when I'm not, I get a silent php error and my theme breaks. The only thing I can guess is that it might be related to user permissions. So I gave not logged in users the ability to Access CiviCRM and View Contributions. I can confirm this works using the dashboard. But I still cant get the API calls to work without breaking the site.</p>
  
  <p>Things I have tried:</p>
  
  <ul>
  <li>Adding <code>'check_permissions' =&gt; false</code> to the call</li>
  <li>Surrounding the calls in a try / catch block</li>
  <li>Switching user context in d7 to the root user and then back again.</li>
  </ul>
  
  <p>Maybe it's not even a permissions problem after all..?</p>
  
  <p>CiviCRM version is 4.7 with Drupal 7.</p>
  
Tags: <api><permissions>
Title: Silent error when accessing php API has not logged in user
ThreadText: # Silent error when accessing php API has not logged in user
  
  (Asked by: username_0 on 2017-05-26)
  
  I'm having a strange problem using the CRM API thru PhP (i.e. not REST or AJAX). I'm trying to build a donations counter which tracks the total amount of contributions made under a specific contribution source. Everything works when I'm logged in. But when I'm not, I get a silent php error and my theme breaks. The only thing I can guess is that it might be related to user permissions. So I gave not logged in users the ability to Access CiviCRM and View Contributions. I can confirm this works using the dashboard. But I still cant get the API calls to work without breaking the site.
  
  Things I have tried:
  
  * Adding `'check_permissions' => false` to the call
  * Surrounding the calls in a try / catch block
  * Switching user context in d7 to the root user and then back again.
  
  Maybe it's not even a permissions problem after all..?
  
  CiviCRM version is 4.7 with Drupal 7.
  
  **Tags:** api, permissions
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2017-07-06. Score: 6 | Accepted answer
  
  The recommended way to include the necessary files to run the php API is to call civicrm\_initialize() before the API call.
  
  
  ---
  
  ### Answer by username_0 on 2017-05-26. Score: 0
  
  If anyone else has this issue, it turns out that the CiviCRM module is not always loaded for not logged in users. Manually including the CRM API file fixed the problem:
      define('DRUPAL_ROOT', '/buildkit/build/CiviCRM');
      define('CIVI_ROOT', DRUPAL_ROOT . '/sites/all/modules/civicrm');
      define('API_PATH', CIVI_ROOT . '/api/api.php');
  
      /* Bootstrap into the Civi environment */
      require_once API_PATH;
      require_once CIVI_ROOT . '/civicrm.config.php';
  
  > **Comments:**
  > **username_1**: you sould also be able to call civicrm_initialize() which will do the same sort of job  
  
  ---
  
  