RSS

Timani I

Open Source web blogging

Include CiviCRM functions from standalone PHP script

April 8th, 2010 by timani, View Comments
Include CiviCRM functions from standalone PHP script

With 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.

PHP
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:

  1. civicrm.settings.php
    1. This is usually in the root of your CiviCRM install. So for Drupal: $drupal_root/sites/all/modules/civicrm/civicrm.settings.php
  2. $civicrm_root/CRM/Core/Config.php
    1. For Drupal based installs: $drupal_root/sites/all/modules/civicrm/CRM/Core/Config.php
  3. 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.

Related posts:

  1. Bootstrap and include Drupal from outside the Drupal root Working on a site that needed to get some pretty...
  2. Zend Framework + WordPress Introduction Often it is the case that you want to...
  3. Open Source PHP Project Management When we were planning on setting up our project management...
  4. Disable the drupal.css When working with style and UI in Drupal there is...

Tags: ,

Register.com

View Comments on “Include CiviCRM functions from standalone PHP script”

You can track this conversation through its atom feed.

  1. Ayo Akinyemi says:

    useful post. thanks

Leave a Reply

blog comments powered by Disqus