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) »
  • Beginner - Need Help with API
Pages: [1]

Author Topic: Beginner - Need Help with API  (Read 1550 times)

benicely

  • I’m new here
  • *
  • Posts: 1
  • Karma: 0
  • CiviCRM version: 4.0.2
  • CMS version: Drupal 7.2
  • MySQL version: 5.1
  • PHP version: 5.3.6
Beginner - Need Help with API
June 13, 2011, 10:24:55 am
I have been tasked with using pulling data out of one database and placing it into CiviCRM using the API.

First of all, I just was given this task on Friday and second of all, I have never used CiviCRM.

I have already read the page located at the flossmanuals api chapter. Unfortunately, the PHP example appears to be showing v2 of the API and not v3.

So after a bunch of trial and error, I was able to finally get what I think is a working example, but I still don't feel like I'm doing this right.

Here are some of my questions which I hope you all can help me figure out...

I wrote a simple PHP get contacts request and the code looks like this:

Apparently, I'm not allowed to put my code in code tags, so you'll have to forgive me.

<?php

$civicrm_root = '../sites/all/modules/civicrm';
require_once $civicrm_root . '/civicrm.config.php';
require_once 'api/api.php';

$params = array(
   'version' => 3,
   'check_permissions' => true
);

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

if (civicrm_api3_error( $results )) {
   echo $results['error_message'];
} else {
   echo '<pre>';
      print_r ($results);
   echo '</pre>';
}

?>

Going to mysite/drupal/custom_api_calls/ getContact.php, I see all my contacts, which is good.

First of all, I'm not sure if I put my PHP page in the correct location since most of the examples I've looked at show the require_once lines not having to go directory up to then traverse to the sub-directories to get the files it needs to run. Also, according to the Wiki, I've seen these two lines

civicrm_initialize();
require_once ' api/api.php ';

I'm not using civicrm_initialize() in my code and it appears to work just fine.

Another question I have is about security. If someone knew the hard link to my PHP page they could run it and see all the contacts. Of course, we want to block anonymous users from doing this, so as you can see in my code above, I put in the line

'check_permissions' => true

That seems to do nothing. I've logged out of CiviCRM, cleared my history, cookies and cache and I can still run the PHP file from a browser. Also, in the admin interface, I've removed all access for anonymous users.

I thought maybe moving my PHP file to \Drupal\sites\all\modules\civicrm might be the cause of permissions not being followed.

As stated above, I was concerned about all the examples not having to traverse the directory structure like I was doing, so I figured moving it to \Drupal\sites\all\modules\civicrm\ getContact.php would do the trick.

I change my app just a little to look like this now:

<?php

require_once 'civicrm.config.php';
require_once ' api / api.php';

$params = array(
  'version' => 3,
  'check_permissions' => true
);

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

if (civicrm_api3_error( $results )) {
   echo $results['error_message'];
} else {
   echo '<pre>';
      print_r ($results);
   echo '</pre>';
}

?>

Going to the page in a browser still allows me to run this page and see the results which is still not what I am expecting.

I know this was a VERY long winded way of asking this, but...

Where exactly should I place my PHP code that calls the APIs?
Why isn't the security working as expected?
Why do some examples have civicrm_initialize(); and others don't?

Thanks,

Brian

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Beginner - Need Help with API
June 13, 2011, 04:47:44 pm
Hi,

To benefit from drupal permission system, you'll need to develop a module with a menu and use the api (with civicrm_initialize) from there. What you are doing is bypassing the CMS, so no access right checking.

This being said if you have access to the shell on your server, I'd strongly suggest you to write a command line, doing it via the web is so much more painful for testing.

check out bin/csv/import.php for an example for instance.

CiviMobile or civiview do implement the api (using smarty, php and ajax calls)

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

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: Beginner - Need Help with API
June 14, 2011, 12:18:16 am
Quote
Where exactly should I place my PHP code that calls the APIs?
That depends on what you want to do and where you do it from. If you are calling the API from your own PHP script, you can place it in a specific directory which you then register as being the one where your CiviCRM custom code is (Global Settings/Directories).

Quote
Why isn't the security working as expected?
Xavier answered that one

Quote
Why do some examples have civicrm_initialize(); and others don't?
Since API v3, you do not have to do the civicrm_initialize as all requirements on that level are handled within the 'api/api.php'



Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Erich Schulz

  • I post frequently
  • ***
  • Posts: 142
  • Karma: 5
    • When no-one understands what you are going on about its time to start a blog
  • CiviCRM version: 4.4
  • CMS version: Drupal 7
  • MySQL version: 5.somthing
  • PHP version: 5.3.3
Re: Beginner - Need Help with API
July 19, 2011, 04:23:21 am
Erik can i just say what a cool name you have :-)

I've just ammended http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs to reflect your comment on civicrm_initialize();

Erich

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Beginner - Need Help with API
July 19, 2011, 07:00:47 am
Ammending the Wiki? Karma +


Brian below - this is an example of the .module file of module called 'custom_membership_report' it registers the url 'customcivireports/contributionmembers' As being a call to function
custom_membership_report_contributions_members in reports.inc (same folder).
 & specifies the drupal permission required

<?php

/**
 * Implementation of hook_menu().
 */
function custom_membership_report_menu() {
  $items = array();
  $items['customcivireports/contributionmembers'] = array(
    'access callback' => 'user_access',
    'access arguments' => array('administer Reports'),
    'file' => 'reports.inc',
    'type' => MENU_CALLBACK,
    'page callback' => 'custom_membership_report_contributions_members',
  );
 
  return $items;
}


The structure is

folder ::
custom_membership_report

contents
custom_membership_report.module (per above
reports.inc (with the function)

custom_membership_report.info

name = Current Membership Report (CSV)
description = Generates a CSV file that shows membership status of those users that have made a contribution in the dates given.
dependencies[] = civicrm
package = "Custom CiviCRM"
core = 6.x


NB - the micro module was written by dgtlmoon
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) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Beginner - Need Help with API

This forum was archived on 2017-11-26.