Include CiviCRM functions from standalone PHP script
Thursday, April 8th, 2010With CiviCRM being such a light, flexible and powerful alternative to use, and as an Open Source utility it makes it even better. Occasionally you will need to include functions, and extend the API.

- Image via Wikipedia
There is a Wiki that is pretty well documented, but even i will admit it will take some getting into, but once you are going then you can do a lot, and i mean a LOT.
Granted that not everyone uses Drupal, or Joomla, and if you are using a
framework like Codeigniter, CakePHP, Symfony, Plone or even another CMS like
WordPress you may want to actually get information related to CiviCRM. To get this done it is a pretty straightforward 3 step process if you have everything install correctly.
First you will need to include the following files:
- civicrm.settings.php
- This is usually in the root of your CiviCRM install. So for Drupal: $drupal_root/sites/all/modules/civicrm/civicrm.settings.php
- $civicrm_root/CRM/Core/Config.php
- For Drupal based installs: $drupal_root/sites/all/modules/civicrm/CRM/Core/Config.php
- include specific php files within the api directory
Once you have located your files above you can then include them in your file to bootstrap Civi. After this is done you will then have access to the global $civicrm_root from within your application.
Here is an example of what a bootstrap file may look like:
// Set the Drupal Root path $drupal_root = '/home/httpdocs/foo'; // Set the Civicrm settings file path $civicrm_settings_path = $drupal_root . '/sites/all/modules/civicrm/civicrm.settings.php'; //Include the CiviCRM settings files require_once $civicrm_settings_path; //Initialize the CRM civicrm_initialize( ); <pre>// Include the Config file require_once $drupal_root. '/CRM/Core/Config.php'; $config =& CRM_Core_Config::singleton( ); // Include any Modules that you may want to extend require_once $drupal_root.'/api/v2/Contribute.php'; // Finally API call time $contribution = civicrm_contribution_get();// now make API call // do whatever you want with your contribution </pre>
Now you can have the CRM running and integrated with virtually any external PHP framework or simple standalone script.
This is great if you have an existing CMS or framework, so rather than a rebuild you can simply extend CiviCRM and get it
to do everything you need.
I think i may actually be interested in a WordPress plugin to try and get some smoother integration. With WordPress 3.0 Beta out it may not be a bad idea.





June 7, 2010 : Zend Framework + MVC is PHP for rapid application development
May 14, 2010 : PHPDoctor – Simple PHP API Documentation
May 13, 2010 : Please sir can i have some more PHP Documentation!
May 12, 2010 : Bash Shell Scripting Tutorial
May 11, 2010 : How To Set Up Database Replication In MySQL – thorough
May 11, 2010 : CSS3 and more – The compatibility Holly Grail?