Answers.Body: <p>As of version 4.6, you can use the <a href="https://civicrm.org/blogs/colemanw/meet-new-api-explorer" rel="nofollow">API Explorer</a> (top menu: Support > Developer > API Explorer) to get code, examples and docs.  You can use the entities and actions to find the right combination to <code>get</code> the info you're looking for. (You can use any number of actions, but it is a live API tool so if you e.g. update an entity, it will change values in the database).</p>
  
  <p>In this case, go to the Explorer then click the tab for Examples.  Choose the entity: <code>Contact</code> and then example: <code>GroupFilterUsingContactAPI</code>.  That generates this code (taken from the Drupal Demo at dmaster.demo.civicrm.org, but the API explorer is not CMS dependent): </p>
  
  <pre><code>&lt;?php
  /**
   * Test Generated example demonstrating the Contact.get API.
   *
   * Get all from group and display contacts.
   *
   * @return array
   *   API result array
   */
  function contact_get_example() {
    $params = array(
      'filter.group_id' =&gt; array(
        '0' =&gt; 1,
        '1' =&gt; 26,
      ),
      'contact_type' =&gt; 'Individual',
    );
  
    try{
      $result = civicrm_api3('Contact', 'get', $params);
    }
    catch (CiviCRM_API3_Exception $e) {
      // Handle error here.
      $errorMessage = $e-&gt;getMessage();
      $errorCode = $e-&gt;getErrorCode();
      $errorData = $e-&gt;getExtraParams();
      return array(
        'error' =&gt; $errorMessage,
        'error_code' =&gt; $errorCode,
        'error_data' =&gt; $errorData,
      );
    }
  
    return $result;
  }
  
  /**
   * Function returns array of result expected from previous function.
   *
   * @return array
   *   API result array
   */
  function contact_get_expectedresult() {
  
    $expectedResult = array(
      'is_error' =&gt; 0,
      'version' =&gt; 3,
      'count' =&gt; 1,
      'id' =&gt; 1,
      'values' =&gt; array(
        '1' =&gt; array(
          'contact_id' =&gt; '1',
          'contact_type' =&gt; 'Individual',
          'contact_sub_type' =&gt; '',
          'sort_name' =&gt; 'man2@yahoo.com',
          'display_name' =&gt; 'man2@yahoo.com',
          'do_not_email' =&gt; 0,
          'do_not_phone' =&gt; 0,
          'do_not_mail' =&gt; 0,
          'do_not_sms' =&gt; 0,
          'do_not_trade' =&gt; 0,
          'is_opt_out' =&gt; 0,
          'legal_identifier' =&gt; '',
          'external_identifier' =&gt; '',
          'nick_name' =&gt; '',
          'legal_name' =&gt; '',
          'image_URL' =&gt; '',
          'preferred_communication_method' =&gt; '',
          'preferred_language' =&gt; 'en_US',
          'preferred_mail_format' =&gt; 'Both',
          'first_name' =&gt; '',
          'middle_name' =&gt; '',
          'last_name' =&gt; '',
          'prefix_id' =&gt; '',
          'suffix_id' =&gt; '',
          'formal_title' =&gt; '',
          'communication_style_id' =&gt; '',
          'job_title' =&gt; '',
          'gender_id' =&gt; '',
          'birth_date' =&gt; '',
          'is_deceased' =&gt; 0,
          'deceased_date' =&gt; '',
          'household_name' =&gt; '',
          'organization_name' =&gt; '',
          'sic_code' =&gt; '',
          'contact_is_deleted' =&gt; 0,
          'current_employer' =&gt; '',
          'address_id' =&gt; '2',
          'street_address' =&gt; '1 my road',
          'supplemental_address_1' =&gt; '',
          'supplemental_address_2' =&gt; '',
          'city' =&gt; '',
          'postal_code_suffix' =&gt; '',
          'postal_code' =&gt; '',
          'geo_code_1' =&gt; '',
          'geo_code_2' =&gt; '',
          'state_province_id' =&gt; '',
          'country_id' =&gt; '',
          'phone_id' =&gt; '',
          'phone_type_id' =&gt; '',
          'phone' =&gt; '',
          'email_id' =&gt; '1',
          'email' =&gt; 'man2@yahoo.com',
          'on_hold' =&gt; 0,
          'im_id' =&gt; '',
          'provider_id' =&gt; '',
          'im' =&gt; '',
          'worldregion_id' =&gt; '',
          'world_region' =&gt; '',
          'individual_prefix' =&gt; '',
          'individual_suffix' =&gt; '',
          'communication_style' =&gt; '',
          'gender' =&gt; '',
          'state_province_name' =&gt; '',
          'state_province' =&gt; '',
          'country' =&gt; '',
          'id' =&gt; '1',
        ),
      ),
    );
  
    return $expectedResult;
  }
  
  /*
  * This example has been generated from the API test suite.
  * The test that created it is called "testGetGroupIDFromContact"
  * and can be found at:
  * https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContactTest.php
  *
  * You can see the outcome of the API tests at
  * https://test.civicrm.org/job/CiviCRM-master-git/
  *
  * To Learn about the API read
  * http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
  *
  * Browse the api on your own site with the api explorer
  * http://MYSITE.ORG/path/to/civicrm/api
  *
  * Read more about testing here
  * http://wiki.civicrm.org/confluence/display/CRM/Testing
  *
  * API Standards documentation:
  * http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
  */
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T16:55:27.083Z
Answers.LastActivityDate: 2016-04-05T04:03:07.843Z
Answers.LastEditDate: 2016-04-05T04:03:07.843Z
Answers.UserDisplayName: Allen Hutchison
Answers.Body: <p>There is also a "less-code" variation of this:</p>
  
  <pre><code>  $result = civicrm_api3('Contact', 'get', array(
          'group' =&gt; array('IN' =&gt; array(1,2,3)), // &lt;-- Group IDs array
      ));
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-06T08:04:53.773Z
Answers.LastActivityDate: 2016-04-06T08:04:53.773Z
Answers.UserDisplayName: yurg
Body: <p>Given the ID of a smart group, what API/BAO/whatever do I need to use to retrieve all contacts belonging to that group? I've been looking through the source code all morning and can't seem to find what I need. Thanks.</p>
  
Tags: <smartgroups>
Title: How to retrieve all contacts belonging to a smart group
ThreadText: # How to retrieve all contacts belonging to a smart group
  
  (Asked by: username_0 on 2016-03-14)
  
  Given the ID of a smart group, what API/BAO/whatever do I need to use to retrieve all contacts belonging to that group? I've been looking through the source code all morning and can't seem to find what I need. Thanks.
  
  **Tags:** smartgroups
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-03-14. Score: 3 | Accepted answer
  
  As of version 4.6, you can use the API Explorer (top menu: Support > Developer > API Explorer) to get code, examples and docs.  You can use the entities and actions to find the right combination to get the info you're looking for. (You can use any number of actions, but it is a live API tool so if you e.g. update an entity, it will change values in the database).
  In this case, go to the Explorer then click the tab for Examples.  Choose the entity: Contact and then example: GroupFilterUsingContactAPI.  That generates this code (taken from the Drupal Demo at dmaster.demo.civicrm.org, but the API explorer is not CMS dependent): 
   array(
        '0' => 1,
        '1' => 26,
      ),
      'contact_type' => 'Individual',
    );
  
    try{
      $result = civicrm_api3('Contact', 'get', $params);
    }
    catch (CiviCRM_API3_Exception $e) {
      // Handle error here.
      $errorMessage = $e->getMessage();
      $errorCode = $e->getErrorCode();
      $errorData = $e->getExtraParams();
      return array(
        'error' => $errorMessage,
        'error_code' => $errorCode,
        'error_data' => $errorData,
      );
    }
  
    return $result;
  }
  
  /**
   * Function returns array of result expected from previous function.
   *
   * @return array
   *   API result array
   */
  function contact_get_expectedresult() {
  
    $expectedResult = array(
      'is_error' => 0,
      'version' => 3,
      'count' => 1,
      'id' => 1,
      'values' => array(
        '1' => array(
          'contact_id' => '1',
          'contact_type' => 'Individual',
          'contact_sub_type' => '',
          'sort_name' => 'man2@yahoo.com',
          'display_name' => 'man2@yahoo.com',
          'do_not_email' => 0,
          'do_not_phone' => 0,
          'do_not_mail' => 0,
          'do_not_sms' => 0,
          'do_not_trade' => 0,
          'is_opt_out' => 0,
          'legal_identifier' => '',
          'external_identifier' => '',
          'nick_name' => '',
          'legal_name' => '',
          'image_URL' => '',
          'preferred_communication_method' => '',
          'preferred_language' => 'en_US',
          'preferred_mail_format' => 'Both',
          'first_name' => '',
          'middle_name' => '',
          'last_name' => '',
          'prefix_id' => '',
          'suffix_id' => '',
          'formal_title' => '',
          'communication_style_id' => '',
          'job_title' => '',
          'gender_id' => '',
          'birth_date' => '',
          'is_deceased' => 0,
          'deceased_date' => '',
          'household_name' => '',
          'organization_name' => '',
          'sic_code' => '',
          'contact_is_deleted' => 0,
          'current_employer' => '',
          'address_id' => '2',
          'street_address' => '1 my road',
          'supplemental_address_1' => '',
          'supplemental_address_2' => '',
          'city' => '',
          'postal_code_suffix' => '',
          'postal_code' => '',
          'geo_code_1' => '',
          'geo_code_2' => '',
          'state_province_id' => '',
          'country_id' => '',
          'phone_id' => '',
          'phone_type_id' => '',
          'phone' => '',
          'email_id' => '1',
          'email' => 'man2@yahoo.com',
          'on_hold' => 0,
          'im_id' => '',
          'provider_id' => '',
          'im' => '',
          'worldregion_id' => '',
          'world_region' => '',
          'individual_prefix' => '',
          'individual_suffix' => '',
          'communication_style' => '',
          'gender' => '',
          'state_province_name' => '',
          'state_province' => '',
          'country' => '',
          'id' => '1',
        ),
      ),
    );
  
    return $expectedResult;
  }
  
  /*
  * This example has been generated from the API test suite.
  * The test that created it is called "testGetGroupIDFromContact"
  * and can be found at:
  * https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContactTest.php
  *
  * You can see the outcome of the API tests at
  * https://test.civicrm.org/job/CiviCRM-master-git/
  *
  * To Learn about the API read
  * http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
  *
  * Browse the api on your own site with the api explorer
  * http://MYSITE.ORG/path/to/civicrm/api
  *
  * Read more about testing here
  * http://wiki.civicrm.org/confluence/display/CRM/Testing
  *
  * API Standards documentation:
  * http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
  */
  
  
  ---
  
  ### Answer by username_2 on 2016-04-06. Score: 2
  
  There is also a "less-code" variation of this:
    $result = civicrm_api3('Contact', 'get', array(
          'group' => array('IN' => array(1,2,3)), // <-- Group IDs array
      ));
  
  
  ---
  
  