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) »
  • API bootstrap?
Pages: [1]

Author Topic: API bootstrap?  (Read 947 times)

pederb

  • I’m new here
  • *
  • Posts: 27
  • Karma: 0
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.22
  • MySQL version: MySql 5.1
  • PHP version: 5.3
API bootstrap?
November 14, 2013, 04:29:36 am
I ran in to some problems with initializing the API from standalone PHP scripts:

PHP Fatal error:  require_once(): Failed opening required 'CRM/Core/Classloader.php'
... in /var/www/secure/sites/all/modules/civicrm/api/class.api.php on line 114


This is how I initialize the API:
require_once '/api/class.api.php' ;
$myapi = new civicrm_api3(array('conf_path'=>$my_civi_config_root'));

After debugging I suspect that the problem is related to Classloader now being initialized in civicrm.settings.php:
It seems as if Classloader is initialized twice. Right?

111:  if (isset($config) && isset($config['conf_path'])) {
112:    define('CIVICRM_SETTINGS_PATH', $config['conf_path'] . '/civicrm.settings.php');
113:    require_once CIVICRM_SETTINGS_PATH;
114:    require_once 'CRM/Core/Classloader.php';
115:    require_once 'api/api.php';
116:    require_once "api/v3/utils.php";
117:    CRM_Core_Classloader::singleton()->register();
118:    $this->cfg = CRM_Core_Config::singleton();
119:    $this->init();
120:  }
121:  else {
122:    $this->cfg = CRM_Core_Config::singleton();
123:  }

If I remove line 114, everything runs fine, but is this a healthy solution...?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API bootstrap?
November 14, 2013, 11:14:02 am
The correct way to initialize the api within drupal is to call

civicrm_initialize();
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

pederb

  • I’m new here
  • *
  • Posts: 27
  • Karma: 0
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.22
  • MySQL version: MySql 5.1
  • PHP version: 5.3
Re: API bootstrap?
November 14, 2013, 11:19:43 am
This part of my solution is in standalone PHP-files that are not dependent on Drupal.
They only need access to the CiviCRM API.

Is it necessary to bootstrap Drupal in this setup?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API bootstrap?
November 14, 2013, 12:11:30 pm
the scripts in the extern directory seem to get away with

Code: [Select]
*/

session_start();

require_once '../civicrm.config.php';
$config = CRM_Core_Config::singleton();

(Although I think my preference would be to bootstrap drupal in case of unexpected interactions)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: API bootstrap?
November 14, 2013, 12:41:01 pm

for 4.5, we are planning on moving completely away from all standalone scripts (except cli.php?). we'll maintain the current scripts for one release.

Eliminates the bootstrapping, security and other complexities

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: API bootstrap?
November 14, 2013, 02:29:47 pm
Pederb, if you're using API and don't want to initialise CiviCRM want to initialize CiviCRM API without bootstrapping the host CMS, you can use the remote interface to talk to it instead.

https://github.com/civicrm/civicrm-core/blob/master/api/class.api.php#L23
@xurizaemon ● www.fuzion.co.nz

pederb

  • I’m new here
  • *
  • Posts: 27
  • Karma: 0
  • CiviCRM version: 4.3.5
  • CMS version: Drupal 7.22
  • MySQL version: MySql 5.1
  • PHP version: 5.3
Re: API bootstrap?
November 15, 2013, 12:35:44 am
Thanks all for your replies.

I have just realized that the actual problem is a misspelling in api/class.api.php, line 114:
114:    require_once 'CRM/Core/Classloader.php';
It should be:
114:    require_once 'CRM/Core/ClassLoader.php';

Anyway, the ClassLoader shouldn't try to load itself by the already instantiated global loader...?
It looks like everything is handled gracefully, so I'll leave it for now.

I'm looking forward to the changes in 4.5.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API bootstrap?
November 15, 2013, 12:48:12 am
Could you create an issue and make a Pull Request on github?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: API bootstrap?
November 15, 2013, 07:56:11 am

i made a PR and committed this for 4.4.2 since it was simple and kinda important to anyone who uses it :)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API bootstrap?

This forum was archived on 2017-11-26.