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 (Moderator: Donald Lobo) »
  • Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
Pages: [1]

Author Topic: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php  (Read 5593 times)

yositsitsu

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 2
  • CiviCRM version: 4.0.4
  • CMS version: joomla 1.6.3
  • MySQL version: 5.5.8-log
  • PHP version: 5.3.5
Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 06:03:32 am
Hi,

Havng set the include_path for the api to work from directories other then home directory, I got a new challenge that I swear
I have no clue how to overcome.

I seem to be the only one that ever got this error/message according to google and the forum and it seems to make no sense to me:

Code: [Select]
You need to define CIVICRM_DSN in civicrm.settings.php
I can see that the settings in civicrm.settings.php have already been segt - I guess by the CiviCRM installation process. So... WTF?

Anyone has an idea?

Thanks!
Yosi

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 06:28:33 am
Hi. Seems that you have broken something.

Can you go back to the out of the box configuration (before you changed include path) and tell what was the initial error message?

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

yositsitsu

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 2
  • CiviCRM version: 4.0.4
  • CMS version: joomla 1.6.3
  • MySQL version: 5.5.8-log
  • PHP version: 5.3.5
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 07:53:14 am
Hey thanks for your quick reply Xavier!

I think the out-of-the-box configuration won't help much since it says it can't find a bunch of phps, which in fact where the reason a changed the include path.

Anyway, here it is:

Code: [Select]
Warning: require_once(api/v3/utils.php) [function.require-once]: failed to open stream: No such file or directory in /home/dream2be/public_html/administrator/components/com_civicrm/civicrm/api/api.php on line 47

Fatal error: require_once() [function.require]: Failed opening required 'api/v3/utils.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/dream2be/public_html/administrator/components/com_civicrm/civicrm/api/api.php on line 47

Hope this helps find the problem :)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 07:58:23 am
don't understand when it happens: is this on a webpage (and which one) or when trying to run a cron, or custom code or??

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

yositsitsu

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 2
  • CiviCRM version: 4.0.4
  • CMS version: joomla 1.6.3
  • MySQL version: 5.5.8-log
  • PHP version: 5.3.5
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 08:14:55 am
Basically, it is a custom php script that resides at 'public_html\templates\custom-php', in case you are familiar with the joomla dir hierarchy.

The script is activated by a from and gets several params by post - email among them.

All the script tries to do currently is a test retrieval of a contact by email, which does not succeed...

Code: [Select]
$params = array(
  'email' => "$email",
  'version' => 3,
);

  $result = civicrm_api( 'contact','get',$params );

  echo $result;
I've attached the whole file.


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 08:27:48 am
Quote from: yositsitsu on August 04, 2011, 08:14:55 am
Basically, it is a custom php script that resides at 'public_html\templates\custom-php', in case you are familiar with the joomla dir hierarchy.


You need to initialise civicrm before being able to use the api, I'm assuming it's not done by default.

    civicrm_initialize();
    require_once 'api/api.php';
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

yositsitsu

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 2
  • CiviCRM version: 4.0.4
  • CMS version: joomla 1.6.3
  • MySQL version: 5.5.8-log
  • PHP version: 5.3.5
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 08:42:04 am
As far as I understand, the initialize function is no longer needed with api v3 - it's necessary when using api v2:
http://wiki.civicrm.org/confluence/display/CRMDOC32/Using+CiviCRM+APIs+-+Code+Snippets

Anyway, i tried to add it and got:
Code: [Select]
Fatal error: Call to undefined function civicrm_initialize()
As I mentioned earlier, my script is running in a custom location and is not ware of civicrm existence - I guess I need to add some path to the
include_path before that will succeed.

Not sure If I need that function though - according to the v3 examples, there is just the include to the api.php - that's it. Here is an examlle
that ships with CiviCRM:

Code: [Select]
function contact_get_example(){
$params = array(
  'email' => 'man2@yahoo.com',
  'version' => 3,
);

  require_once 'api/api.php';
  $result = civicrm_api( 'contact','get',$params );

  return $result;
}

Thanks so much for your efforts mate!
I hope someone here has already done it and can give some priceless pointer.

Yosi

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 09:51:56 am
Quote from: yositsitsu on August 04, 2011, 08:42:04 am
As far as I understand, the initialize function is no longer needed with api v3
Anyway, i tried to add it and got:
Code: [Select]
Fatal error: Call to undefined function civicrm_initialize()

Well it needs to initialise a few things (path is one of them). And it's most often done by default in drupal, don't know joomla enough, but doesn't seem to be the case.

I don't know what to do to initialize it on joomla. you might want to ask on the joomla forum
-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: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 04, 2011, 11:40:09 am

check the cron scripts to see how to initialize civicrm, basically u need to include civicrm.settings.php and then do:

Code: [Select]

require_onnce 'FULL_PATH OR SOMETHING THAT WILL FIND THIS/civicrm.settings.php';

require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton( );

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

yositsitsu

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 2
  • CiviCRM version: 4.0.4
  • CMS version: joomla 1.6.3
  • MySQL version: 5.5.8-log
  • PHP version: 5.3.5
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 05, 2011, 04:18:11 am
Quote from: xavier on August 04, 2011, 09:51:56 am
Well it needs to initialise a few things (path is one of them). And it's most often done by default in drupal, don't know joomla enough, but doesn't seem to be the case.

I don't know what to do to initialize it on joomla. you might want to ask on the joomla forum

Xavier, thanks for the tips but I don't get something. Can you please clarify the following?

Even if Joomla/Drupal initialize an environment for communicating with CiviCRM (which I'm sure both of them do), how can that help a standalone PHP script like mine?
As I mentioned, I've written a custom script that resides in a custom PATH and is absolutely unaware neither of Joomla nor of CRM.

Where do you write your PHP code when you say Drupal does init work by default? Do you write your code in a module within Drupal, thus reusing the connection already setup instead of initializing it by yourself?

It seems I'm doing things the hard way... :)

After I get how thin API things work in Joomla, I will be glad to document it in an easy to understand way - currently it is not the case.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 05, 2011, 04:30:40 am
Hi,

Lobos got the solution:

Quote from: Donald Lobo on August 04, 2011, 11:40:09 am

check the cron scripts to see how to initialize civicrm, basically u need to include civicrm.settings.php and then do:

Code: [Select]

require_once 'FULL_PATH OR SOMETHING THAT WILL FIND THIS/civicrm.settings.php';

require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton( );

lobo

That will set up the path and whatever is needed to make your code aware of civi. As for wondering why instantiating a config class using a singleton is the most obvious way of doing it and doesn't smell like a weird side effect... ;)

Yes, when you write a drupal module, the code is already aware of civicrm and you don't have to initialise.

As you seem to bypass all the joomla control and session, be sure you don''t expose more data than you'd want.

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

yositsitsu

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 2
  • CiviCRM version: 4.0.4
  • CMS version: joomla 1.6.3
  • MySQL version: 5.5.8-log
  • PHP version: 5.3.5
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 06, 2011, 05:42:10 am
Quote from: Donald Lobo on August 04, 2011, 11:40:09 am

check the cron scripts to see how to initialize civicrm, basically u need to include civicrm.settings.php and then do:

Code: [Select]

require_onnce 'FULL_PATH OR SOMETHING THAT WILL FIND THIS/civicrm.settings.php';

require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton( );

lobo

Much thanks lobo - your suggestion did the work for me!
Did you get that from documentation or should I add it there?

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: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 06, 2011, 06:23:56 am

can you please add this to the wiki documentation

thanx

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

yositsitsu

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 2
  • CiviCRM version: 4.0.4
  • CMS version: joomla 1.6.3
  • MySQL version: 5.5.8-log
  • PHP version: 5.3.5
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 06, 2011, 06:50:57 am
Added at http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs
under the title of:
"Binding from a standalone custom PHP script (Joomla)"

My first documentational contribution to Civi - feel free to correct me :)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php
August 06, 2011, 07:19:04 am
Looks good to me, thanks!

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Using the CiviCRM API - You need to define CIVICRM_DSN in civicrm.settings.php

This forum was archived on 2017-11-26.