Answers.Body: <p>You need to do it the other way around. Membership is the main entity and then you join on Contact. The api explorer can help put it together but you're looking for something like:</p>
  
  <pre><code>$result = civicrm_api3('Membership', 'get', [
    'sequential' =&gt; 1,
    'return' =&gt; ["contact_id.id", "contact_id.display_name", "membership_type_id"],
  ]);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-18T18:22:24.690Z
Answers.LastActivityDate: 2019-01-18T18:22:24.690Z
Answers.UserDisplayName: Demerit
Body: <p>I am trying to create a lists of contact along with the membership type associated with each contact. Below is the code I have:</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'get', array(
      'sequential' =&gt; 1,
      'return' =&gt; "tag,display_name,id,image_URL",
      'contact_type' =&gt; "Organization",
      'options' =&gt; array('limit' =&gt; 300),
    ));
  </code></pre>
  
  <p>I am not sure how to do join api call to both contact and membership, so that I can get all detail in one single api call.</p>
  
  <p>so I am wondering if anyone can help.</p>
  
  <p>So, far I have created another api call to each contact list returned form the above api call, like this:</p>
  
  <pre><code>foreach($result['values'] as $member){
      $ufmatch = civicrm_api3('Membership', 'get', array(
        'sequential' =&gt; 1,
        'contact_id' =&gt; $member['id'],
      ));
      $member['member_type'] = $ufmatch['values'][0]['membership_name'];
  
      $nodes[] = array(
        'node' =&gt; $member,
      );
    }
  
  $members = array(
      'nodes' =&gt; $nodes,
    );
  
    return drupal_json_output($members);
  </code></pre>
  
  <p>But there are hundreds of contact list. So, it takes almost 2-3 mins load all data.</p>
  
  <p>Can anyone please guide me in a right direction. Thanks in advance. </p>
  
Tags: <api>
Title: Fetch membership type along with the contact details with civicrm_api
ThreadText: # Fetch membership type along with the contact details with civicrm_api
  
  (Asked by: username_0 on 2019-01-18)
  
  I am trying to create a lists of contact along with the membership type associated with each contact. Below is the code I have:
  
  ```
  $result = civicrm_api3('Contact', 'get', array(
      'sequential' => 1,
      'return' => "tag,display_name,id,image_URL",
      'contact_type' => "Organization",
      'options' => array('limit' => 300),
    ));
  
  ```
  
  I am not sure how to do join api call to both contact and membership, so that I can get all detail in one single api call.
  
  so I am wondering if anyone can help.
  
  So, far I have created another api call to each contact list returned form the above api call, like this:
  
  ```
  foreach($result['values'] as $member){
      $ufmatch = civicrm_api3('Membership', 'get', array(
        'sequential' => 1,
        'contact_id' => $member['id'],
      ));
      $member['member_type'] = $ufmatch['values'][0]['membership_name'];
  
      $nodes[] = array(
        'node' => $member,
      );
    }
  
  $members = array(
      'nodes' => $nodes,
    );
  
    return drupal_json_output($members);
  
  ```
  
  But there are hundreds of contact list. So, it takes almost 2-3 mins load all data.
  
  Can anyone please guide me in a right direction. Thanks in advance.
  
  **Tags:** api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-18. Score: 1
  
  You need to do it the other way around. Membership is the main entity and then you join on Contact. The api explorer can help put it together but you're looking for something like:
  
  ```
  $result = civicrm_api3('Membership', 'get', [
    'sequential' => 1,
    'return' => ["contact_id.id", "contact_id.display_name", "membership_type_id"],
  ]);
  
  ```
  
  
  ---
  
  