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 with Contact info fully populated
Pages: [1]

Author Topic: API with Contact info fully populated  (Read 983 times)

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
API with Contact info fully populated
August 08, 2012, 06:04:31 am
I'm writing a Drupal integration that will hook into civicrm_member_roles.module when a membership gets updated to fire off some xml output. I am first loading the membership and contact info from the API. This is working well, but I don't get quite all the contact info that I want (just getting id, name, email -- I want to also load the address, phone, and employer info).

Code: [Select]
function _civicrm_member_sync_xml($cid){
 //Get all memberships of contact
  civicrm_api_include('membership', false, 2);
  $memberships = civicrm_contact_memberships_get( $cid );
  $contactMemberships = CRM_Utils_Array::value( $cid, $memberships, array( ) );
 
  // Load the contact
  civicrm_api_include('contact', false, 2);
  $contact = civicrm_api('Contact','Get',array('contact_id' => $cid, 'version' =>3));
 

}

Seems I probably need a CRM_Utils_Array call related to the contact once it's loaded...I have been unable to find an example of which one I need. Can someone help me out and then I'll update the wiki with my example? Thanks so much.

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 with Contact info fully populated
August 08, 2012, 12:04:51 pm
Hi,

Check out the api explorer, the default url has an example of that
you need to set up the return param

return=email,phone,custom_42...

What is this civicrm_api_include? You don't need it, civicrm should be available (if not, you can call civicr_initialize() and it should

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API with Contact info fully populated
August 08, 2012, 01:40:38 pm
These lines

  civicrm_api_include('membership', false, 2);
  $memberships = civicrm_contact_memberships_get( $cid );


Have been removed from the d6 version now & hopefully the d7 version.

civicrm_api_include has been removed from 4.2 I think
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

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: API with Contact info fully populated
August 09, 2012, 08:05:10 am
Thanks both for your help, I ended up using the following calls:

Code: [Select]
function _civicrm_member_roles_sync_xml($cid){
  //Load the contact info first
  $results= civicrm_api("Contact","get", array ('version' =>'3', 'contact_id' => $cid));
 
  // take just what we want of the contact
  foreach($results['values'] as $contact){
   
    $contact_array = array('key' => $contact['contact_id'], 'first_name' => $contact['first_name'], 'last_name' => $contact['last_name'], 'email' => $contact['email'], 'employment' => $contact['current_employer'], 'phone' => $contact['phone'], 'address' => $contact['street_address']. " " . $contact['supplemental_address_1'], 'city' => $contact['city'], 'state' => $contact['state_province'], 'postal_code' => $contact['postal_code'], 'country' => $contact['country']);
  }
 
  // Now load the membership that has changed
  $member_results=civicrm_api("Membership","get", array ('version' =>'3', 'contact_id' => $contact['contact_id']));
  foreach($member_results['values'] as $member){
    $member_array = array('member_type' => $member['membership_name'], 'member_expiration' => $member['end_date']);   
  } 
 
  // Prepare the final array that will be used for xml output
  $xml_array = array_merge_recursive($contact_array, $member_array);
 
  return $xml_array();
 
}

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API with Contact info fully populated
August 09, 2012, 01:52:28 pm
Hi,

If you chained that it would look like

$results= civicrm_api("Contact","get", array ('version' =>'3', 'contact_id' => $cid, 'api.membership.get' => 1));

For each contact returned it would do a membership.get call

Eileen

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

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 with Contact info fully populated
August 09, 2012, 11:23:58 pm
Hi,

You should use the return='first_name,last_ name...'. By default, it's likely it's fetching more fields that you need.

If you add as a param sequential=1, you can avoid the foreach that flatten values.

If you have any suggestion on how to promote these features of the API. please tell me ;)
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: API with Contact info fully populated
August 10, 2012, 08:15:36 am
Thanks to you both, this is very helpful. One question -- I can't seem to get the return param to work for multiple fields. For example:

Code: [Select]

 $results= civicrm_api("Contact","get", array ('version' =>'3', 'contact_id' => $cid, 'api.membership.get' => 1, 'return' => 'contact_id, last_name, first_name'));
 

does not return those three fields. What am I doing wrong?

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: API with Contact info fully populated
August 10, 2012, 08:17:35 am
dgg just helped me out on irc. so i had to do the following:

Code: [Select]
$results= civicrm_api("Contact","get", array ('version' =>'3', 'contact_id' => $cid, 'api.membership.get' => 1, 'return_first_name' => TRUE, 'return_last_name' => TRUE));
i'll post this up to the api docs as well as i could not find this in the documentation.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API with Contact info fully populated
August 10, 2012, 03:23:14 pm
That's actually the old syntax - although it should still work - but I don't think the return syntax options become consistent until 4.2 - at which point they all should work on all api but we are encouraging:

'return' => array('first_name', 'last_name')

Note that api.membership.get => 1 is equivalent to api.membership.get => array() (where contact_id, sequential & version are set for you). You can also pass in

'api.membership.get' => array('return' => 'membership_type_id')

- there are other variations on this type of thing in the examples folder.

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

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: API with Contact info fully populated
August 11, 2012, 08:18:43 am
Thanks Eileen!

I forgot to mention I am on 3.4, sorry about that. But this is good to know the differences -- I have posted up some return examples on the wiki here: http://wiki.civicrm.org/confluence/display/CRMDOC41/CiviCRM+Public+APIs

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

This forum was archived on 2017-11-26.