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) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • how do we call the civicrm api from within a drupal module (php)
Pages: [1]

Author Topic: how do we call the civicrm api from within a drupal module (php)  (Read 3784 times)

antidumbdown

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.0.6
  • CMS version: Drupal 7.8
  • MySQL version: 5.5.9
  • PHP version: 5.3.6
how do we call the civicrm api from within a drupal module (php)
October 17, 2011, 02:32:04 am
Please excuse me if this has been discussed, seems like it must be used often enough, but i can't find or figure out the right way forward.


I use a custom drupal module to collect some form field data from users and put that data into a DB, lat in the script I want to add that data to the civicrm db. most of the documentation i come across says simply to require the api.php file and call cicirm_api function... But this alone does not work from within a standard drupal module. What is the best way to get this data into a new civicrm contact?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: how do we call the civicrm api from within a drupal module (php)
October 17, 2011, 02:52:17 am
What do you mean by it doesn't work? Can you share the code or detail?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

antidumbdown

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.0.6
  • CMS version: Drupal 7.8
  • MySQL version: 5.5.9
  • PHP version: 5.3.6
Re: how do we call the civicrm api from within a drupal module (php)
October 17, 2011, 11:07:49 am
Hi xavier,

Following the recommendation from floss developer guide on api usage, i require_once the api file and then tried to use the civicrm_api function call from within a drupal module within a hook_node_view hook, when i do this i get the following error;

Code: [Select]
Fatal error: require_once() [function.require]: Failed opening required 'api/api.php'
This suggests that the api is not available to drupal, either i set something up wrong or i am doing something wrong because it would seem that the API should be available by the time I am in a hook_node_view.

Next, I edited the path in the require_once to be more specific like so

Code: [Select]
require_once('sites/all/modules/civicrm/api/api.php');
Now my error has changed, this time its:

Code: [Select]
Fatal error: require_once() [function.require]: Failed opening required 'api/v3/utils.php'
Am i missing something during setup?

« Last Edit: October 17, 2011, 11:17:27 am by antidumbdown »

antidumbdown

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.0.6
  • CMS version: Drupal 7.8
  • MySQL version: 5.5.9
  • PHP version: 5.3.6
Re: how do we call the civicrm api from within a drupal module (php)
October 17, 2011, 11:12:12 am
Ok, some progress, but it does not feel right.
I read the posts on this thread: http://forum.civicrm.org/index.php/topic,20989.msg87937.html#msg87937

and then pasted in this code into my modules hook_node_view

   
Code: [Select]
require_once 'sites/default/civicrm.settings.php';

    require_once 'CRM/Core/Config.php';
    $config = CRM_Core_Config::singleton( );
   
    $results = civicrm_api("Contact","create", $params);
    print_r($results);

It worked. But again it does not feel right, can someone point me in the right direction. what is the approach I should be taking to access the api from within a drupal module.
« Last Edit: October 17, 2011, 11:16:00 am by antidumbdown »

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: how do we call the civicrm api from within a drupal module (php)
October 17, 2011, 01:39:04 pm

can u try this:

Code: [Select]
  civicrm_initialize( );

  $results = civicrm_api("Contact","create", $params);
  print_r($results); 

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

antidumbdown

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.0.6
  • CMS version: Drupal 7.8
  • MySQL version: 5.5.9
  • PHP version: 5.3.6
Re: how do we call the civicrm api from within a drupal module (php)
October 17, 2011, 02:14:40 pm
Hi lobo, that worked.  :)

So is this the recommended way to interact with the API from within a drupal module?

Thank You
chris

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: how do we call the civicrm api from within a drupal module (php)
October 17, 2011, 02:34:00 pm

IMO, yes :)

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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: how do we call the civicrm api from within a drupal module (php)
November 11, 2011, 10:37:01 am
Interesting, so the require_once is no longer needed? As of what version is this file automatically loaded?
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: how do we call the civicrm api from within a drupal module (php)
November 11, 2011, 08:27:51 pm
As of Xavier rant 43.1  ;D

(It was quite a while ago)
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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • how do we call the civicrm api from within a drupal module (php)

This forum was archived on 2017-11-26.