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.

2 Answers 2


The recommended way to include the necessary files to run the php API is to call civicrm_initialize() before the API call.


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';
  • 1
    you sould also be able to call civicrm_initialize() which will do the same sort of job
    – Seamus Lee
    Commented May 26, 2017 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.