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 Newbie questions
Pages: [1]

Author Topic: API Newbie questions  (Read 1330 times)

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
API Newbie questions
December 04, 2012, 04:02:44 pm
I am starting working on doing some integration between a custom web application (LAMP) and CiviCRM.  My first task is to insert contact data into CiviCRM.  I have the basics worked out but I have some particular questions and issues.

First, the issue of the login to CiviCRM.  This is going to create a session on the CiviCRM site, lasting of unknown duration.  There may need to be several API calls to Civi during the user's session.  How to manage the session?  The custom app doesn't really know when the session on the other server expires.  Is it best to just do a login each time?

Second, I have basic data being inserted.  However two items confuse me.  According to the API Explorer, email address shouldn't be available, however when I provide that item it does get saved.  Conversely, according to the API explorer, Organization Name should be available, but when I provide it, it doesn't get saved.  What am I missing here?  Since this is my first crack at it I am not confident that I am writing it correctly, and at the same time I am not entirely confident that the API explorer is accurate.

Here's my code:

Code: [Select]
public function logonCivicrm()
    {

        $logindata = json_decode(file_get_contents("http://crm.site.org/sites/all/modules/civicrm/extern/rest.php?json=1&q=civicrm/login&name=admin&pass=xxxxx&key=xxxxxxxxxxxxxxxxxxxxx"),true);
        $apikey = $logindata['api_key'];
        $sessionid = $logindata['PHPSESSID'];

        SessionUtil::setVar('civiAPIkey', $apikey);
        SessionUtil::setVar('civiSessionID', $sessionid);
    }

    public function createCivicrm($args)
    {
        foreach ($args['persondata'] as $k => $v) {
            $fields .= "&$k=" . urlencode($v);
        }
        //$apikey = SessionUtil::getVar('civiAPIkey');
        //if (!$apikey){
        $ok = ModUtil::apiFunc('People', 'civicrm', 'logonCivicrm');
        $apikey = SessionUtil::getVar('civiAPIkey');
        if (!$apikey){
            return false;
        }
        // }
        $url = "http://crm.site.org/sites/all/modules/civicrm/extern/rest.php?json=1&entity=Contact&action=create&key=xxxxxxxxxxxxxxxxxxxxx&api_key=$apikey&contact_type=Individual";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($args['[persondata']) + 2);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);

    }

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API Newbie questions
December 04, 2012, 04:37:29 pm
The code below is specifying contact_type = 'Individual' - in which case 'organization_name' would not be valid as it doesn't apply to individuals.

Adding api_action=get (as appropriate) will give you more specific information about fields
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

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
Re: API Newbie questions
December 04, 2012, 04:41:56 pm
Of course.  I was thinking this was the organization of the individual.  This is holdover thinking from doing a Civi import in which it could handle data from different tables at one time.

Thanks

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
Re: API Newbie questions
December 04, 2012, 10:55:45 pm
I have some custom fields I need to insert when creating a Contact.  When I use the API Explorer and select the field as an option and enter a test value, the URL is modified to append "&undefined=test".

Undefined?  Is there a limitation for custom fields?  This would be a serious problem...

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: API Newbie questions
December 04, 2012, 11:28:50 pm
I would recommend you use the CustomValue API to add custom data. The entity_id should be the contact_id. Example:
Code: [Select]

$customColumn = $customField['customfield'];
civicrm_api( 'CustomValue', 'create', array (
'version' => '3',
'entity_id' => $contactID,
$customColumn => $daoPers->personeel_id ) );
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
Re: API Newbie questions
December 04, 2012, 11:34:18 pm
Thanks.  Does it make a difference that this is from an external web site using REST, and civicrm_api() function isn't available?  API Explorer seems to describe the same field names for CustomField as you have listed here.

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 Newbie questions
December 04, 2012, 11:40:45 pm
It doesn't make any difference, but a few "cosmetic" ones on the syntax.

The big difference is that by default on php it doesn't use the permission of the user but on REST it always does.

If you plan to use the REST from php or node.js on the consumer site, they are already classes to abstract the calls

for php it's already part of civi: copy api/class.api.php to the client (read the code, comments at the start explain how to initialise it)

for node.js
https://npmjs.org/package/civicrm

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

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
Re: API Newbie questions
December 05, 2012, 03:21:41 pm
I am seeing an issue with states and countries as parts of addresses.

It seems that via the API I can't submit actual strings for this, i.e. "California", "United States".  I need to submit the record ids of these values as matched in the CiviCRM system, i.e, "1004", "1228".

Which means I have to replicate the CiviCRM state/country database in the external app and provide an interface for selecting those values?!?

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 Newbie questions
December 05, 2012, 04:31:59 pm
Hi,

Haven't used the state, but for countries, works best using the iso code (eg. "us").

you could use the api to fetch the list of countries or states using the api if needed.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
Re: API Newbie questions
December 05, 2012, 04:52:41 pm
I'm still not able to get the CustomValues working.  Here's what I have:

Code: [Select]
// create custom: principalsname
        $fields = "";
        $customfield['entity_id'] = 1234;
        $customfield['principals_name_140'] = 'Claire';
        foreach ($customfield as $k => $v) {
            $fields .= "&$k=" . urlencode($v);
        }
       
        $url = "http://crm.site.org/sites/all/modules/civicrm/extern/rest.php?json=1&entity=CustomValue&action=create&key=xxxxxxxxxxxxxxxxxxxx&api_key=12345678&version=3";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($customfield) + 6);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
       
        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        $principalsnamerst = json_decode($result, true);

The result is "Unknown Error".  I've tried working with this in the API explorer as well, using:

/civicrm/ajax/rest?json=1&sequential=1&debug=1&entity=CustomValue&action=create&Principals_Name=Claire&entity_id=4578&version=3

here's the json output:

Quote
{
   "is_error":1,
   "error_message":"Unknown error",
   "trace":"#0 \/home\/crm\/public_html\/sites\/all\/modules\/civicrm\/CRM\/Core\/BAO\/CustomValueTable.php(580): CRM_Core_Error::createAPIError('Unknown error')\n#1 \/home\/crm\/public_html\/sites\/all\/modules\/civicrm\/api\/v3\/CustomValue.php(111): CRM_Core_BAO_CustomValueTable::setValues(Array)\n#2 \/home\/crm\/public_html\/sites\/all\/modules\/civicrm\/api\/api.php(65): civicrm_api3_custom_value_create(Array)\n#3 \/home\/crm\/public_html\/sites\/all\/modules\/civicrm\/CRM\/Utils\/REST.php(426): civicrm_api('CustomValue', 'create', Array)\n#4 \/home\/crm\/public_html\/sites\/all\/modules\/civicrm\/CRM\/Utils\/REST.php(655): CRM_Utils_REST::process(Array, false)\n#5 [internal function]: CRM_Utils_REST::ajax(Array)\n#6 \/home\/crm\/public_html\/sites\/all\/modules\/civicrm\/CRM\/Core\/Invoke.php(186): call_user_func(Array, Array)\n#7 \/home\/crm\/public_html\/sites\/all\/modules\/civicrm\/drupal\/civicrm.module(500): CRM_Core_Invoke::invoke(Array)\n#8 [internal function]: civicrm_invoke('ajax', 'rest')\n#9 \/home\/crm\/public_html\/includes\/menu.inc(517): call_user_func_array('civicrm_invoke', Array)\n#10 \/home\/crm\/public_html\/index.php(21): menu_execute_active_handler()\n#11 {main}"
}

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: API Newbie questions
December 06, 2012, 04:22:26 am
Hi,

I have not checked this, but I would expect you would need to use 'custom_140' rather than 'principals_name_140'. Give that a try?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
Re: API Newbie questions
December 06, 2012, 09:17:08 am
Thanks!  That worked! 

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: API Newbie questions
December 06, 2012, 09:17:53 am
Glad it did, good luck with the rest  ;D
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) »
  • API Newbie questions

This forum was archived on 2017-11-26.