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: Contact - get all contact's emails
Pages: [1]

Author Topic: API: Contact - get all contact's emails  (Read 920 times)

dbl81

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: drupal 7
  • MySQL version: 5
  • PHP version: 5.2
API: Contact - get all contact's emails
February 27, 2014, 11:13:43 am
Hi all,
I'm writing down an extension for roundcube that uses the rest apis of civiCRM. Now I'm a little bit stucked on the use of Contact api: I would get all emails of a contact while the api returns only one. I can use the email api (I know the contact_id) but it's another post I have to send to my civiCRM.
Do you know a way to get all emails of a contact using only the contact api?

Thank you all.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: API: Contact - get all contact's emails
February 27, 2014, 11:21:57 am
CiviCRM 4.5 supports multiple api calls in a single request, which is nice but that version hasn't been released yet.
But you could achieve the same thing using chaining. So add api.email.get={} to your request. That should do it.
Try asking your question on the new CiviCRM help site.

dbl81

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: drupal 7
  • MySQL version: 5
  • PHP version: 5.2
Re: API: Contact - get all contact's emails
March 02, 2014, 02:42:56 am
Coleman, thank you for you answer.

I tried to add api.email.get={} as param in my post, but now the response contains only all contact's emails. I would get contact details + all contact's emails with one call.
My params at now are:
entity = contact
action = get
contact_id = x
api.email.get = {}

Am I doing something wrong?

Thank you.

dbl81

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: drupal 7
  • MySQL version: 5
  • PHP version: 5.2
Re: API: Contact - get all contact's emails
March 02, 2014, 03:40:40 am
I have an update.

If I try to invoke a rest api asking an xml response I have a right answer:

Code: [Select]
<?xml version="1.0"?>
      <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  count="1" >
      <Result>
    <contact_id>3982</contact_id>
    BLABLA
    <api_email_get>
        <is_error>0</is_error>
        <undefined_fields>
            <0>entity_id</0>
            <1>entity_table</1>
            <2>contact_id</2>
            <3>api.has_parent</3>
        </undefined_fields>
        <version>3</version>
        <count>3</count>
        <values>
            <0>
                <id>4031</id>
                <contact_id>3982</contact_id>
            </0>
            <1>
                <id>4032</id>
                <contact_id>3982</contact_id>
            </1>
            <2>
                <id>10862</id>
                <contact_id>3982</contact_id>
            </2>
        </values>
    </api_email_get>
</Result>
</ResultSet>

PHP code:

Code: [Select]
        $fields['key'] = 'blabla';
        $fields['api_key'] = 'blabla';
$fields['entity'] = 'contact';
$fields['action'] = 'get';
$fields['debug'] = 1;
$fields['api.email.get'] = array('return' => 'id');
$fields['id'] = 3982;
        // do post to rest interface


If I try to invoke a rest api asking a json response I have the following problem:

Code: [Select]
{
"is_error":0,
"undefined_fields":["api_email_get"],
"version":3,
"count":1,
"id":3982,
"values":{
"3982":{
                  blabla
}
}

PHP code:

Code: [Select]
        $fields['key'] = 'blabla';
        $fields['api_key'] = 'blabla';
$fields['entity'] = 'contact';
$fields['action'] = 'get';
$fields['debug'] = 1;
$fields['json'] = 1;
$fields['api.email.get'] = array('return' => 'id');
$fields['id'] = 3982;
        // do post to rest interface

Why? :(

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: Contact - get all contact's emails
March 02, 2014, 05:03:15 am
Hi,

Can you try using api/class.api.php to do the call? it should be simplier and take care of hiding all the rest issues (check out the source code, it gives examples)

advice: you really should specify a return="first_name,last_name,wahtever_field_you_need", because by default it fetches tons of fields that you don't need. a bit like you don't do SELECT * in sql.

you should have a api_email_get that contains the fields under each contact returned. xml return format seems odd (but I never used it, so can't tell you more about it)

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

dbl81

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: drupal 7
  • MySQL version: 5
  • PHP version: 5.2
Re: API: Contact - get all contact's emails
March 09, 2014, 04:57:32 pm
finally I did it. Sorry for posting so late, but I was really busy. Following my example:

Code: [Select]
$fields['entity'] = 'contact';
$fields['action'] = 'get';
$fields['json'] = '1';
$fields['sequential'] = '1';
// the id for contact retrieving
$fields['id'] = <<contact_id>>;
// api chain to retrieve all contact's emails
$fields['api.email.get'] = array('return' => 'id, email, location_type_id, is_primary');
// api chain to retrieve all contact's phones
$fields['api.phone.get'] = array('return' => 'id, phone, location_type_id, is_primary, phone_type_id, phone_numeric');
// api chain to retrieve contact's groups
$fields['api.GroupContact.get'] = array('contact_id' => <<contact_id>>);

Thank you all for your help.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API: Contact - get all contact's emails

This forum was archived on 2017-11-26.