CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • SOLVED: How to use the CiviCRM API outside the CiviCRM install?
Pages: [1]

Author Topic: SOLVED: How to use the CiviCRM API outside the CiviCRM install?  (Read 3252 times)

stuart

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6
  • MySQL version: 5.0.77
  • PHP version: 5.3
SOLVED: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 02:41:08 am
This is my first time using the API, so I'm probably doing something rather obvious wrong. I've tried to mimic the examples, but something must be different.

I've added a "custom_scripts" folder in the root of my Drupal install, and am writing a script to do a one off task with CiviCRM.

My code:

Code: [Select]
<?php

// Show any errors
error_reporting(E_ALL);
ini_set('display_errors', TRUE);

// Get access to CiviCRM API
chdir('/var/www/drupal/sites/all/modules/civicrm/');
require_once(
'api/api.php');

$result = civicrm_api('uf_match', 'get', array('version' => 3, 'uf_id' => 10));
var_dump($result);

The error I get:

Code: [Select]
Warning: require_once(Log.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/drupal/sites/all/modules/civicrm/CRM/Core/Config.php on line 40

Fatal error: require_once() [function.require]: Failed opening required 'Log.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/drupal/sites/all/modules/civicrm/CRM/Core/Config.php on line 40
« Last Edit: November 02, 2012, 06:38:15 am by Erik Hommel »

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 03:39:16 am
Perhaps you need to add a civicrm_initialize() before your API call?
So:
Code: [Select]
require_once('api/api.php');
civicrm_initialize();
$result = civicrm_api('uf_match', 'get', array('version' => 3, 'uf_id' => 10));
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

stuart

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6
  • MySQL version: 5.0.77
  • PHP version: 5.3
Re: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 03:51:14 am
Thanks for the suggestion. PHP can't locate the function however:

Code: [Select]
Fatal error: Call to undefined function civicrm_initialize()

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 03:54:46 am
hmmm looks like it does not know CiviCRM...if you want to call the API without initializing CiviCRM you probably need to use the REST to call the API? What version CiviCRM are you using? And I am assuming you have read the delveper guide http://book.civicrm.org/developer?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

stuart

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6
  • MySQL version: 5.0.77
  • PHP version: 5.3
Re: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 04:05:27 am
I presumed that just including the API class would let you use the CiviCRM functions even outside of the main install. Is there no equivalent of the Drupal bootstrap, which lets you use Drupal functions from anywhere on your webserver?

I'm on CiviCRM 3.4.8. I've read the guides/looked at examples, but they don't mention working outside the main Civi install. I could use REST as a last resort, but that would make it unnecessarily complicated having to process JSON/XML responses.
« Last Edit: November 02, 2012, 04:07:07 am by stuart »

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 05:02:10 am
I think this will help:
Code: [Select]
if (!isset($config)) {
    require_once($_SERVER['DOCUMENT_ROOT'].
'/sites/default/civicrm.settings.php');
    require_once 'CRM/Core/Config.php';
    $config =CRM_Core_Config::singleton( );
}
require_once 'api/api.php';
$result = civicrm_api('uf_match', 'get', array('version' => 3, 'uf_id' => 10));

Which is basically what a civicrm_initialize does...
« Last Edit: November 02, 2012, 05:04:06 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

stuart

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6
  • MySQL version: 5.0.77
  • PHP version: 5.3
Re: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 06:31:03 am
Thanks very much for your help, working now.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: How to use the CiviCRM API outside the CiviCRM install?
November 02, 2012, 06:37:08 am
Pleasure, glad it works!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • SOLVED: How to use the CiviCRM API outside the CiviCRM install?

This forum was archived on 2017-11-26.