18

I'm making a normal api call such as:

$params = array(
  'version' => 3,
  'sequential' => 1,
  'membership_type' => 1
);
$result = civicrm_api('Membership', 'get', $params);

But I only get 25 results back.

How do I get more than that, and how do I ensure I can retrieve all results, if say there was 5,000?

16

To return more than 25 results you need to add options.limit to your api call:

$params = array(
  'version' => 3,
  'sequential' => 1,
  'membership_type' => 1,
    'options' => array(
    'limit' => 100,
   ),
);
$result = civicrm_api('Membership', 'get', $params);

For the second part, we want to be able to retrieve say 5,000 records. We can't really set rowCount to 5,000 because depending on the size of data, we run the risk of timing out.

The solution here is to page the results using a parameter 'options.offset':

$params = array(
  'version' => 3,
  'sequential' => 1,
  'membership_type' => 1,
  'options' => array(
    'limit' => 100,
    'offset' => 100 // offset value is equal to the number of results to skip
  ),
);

For this to work, we decide on what our ideal page size is let's say 100. We set options.limit to 100, and then we increment offset by our page size (100) on each call, until we no longer get results.

How do we know when we're done? Check the value of count in the returned result. When the returned count is less than the page size, stop.

  $memberships = array();
  // Initialise our pagesize and offset
  $page_size = 100;
  $offset = 0;
  // Start out loop
  do {
    // Call the api
    $results = civicrm_api(
      'Membership',
      'get',
      array(
        'version'         => 3,
        'sequential'      => 1,
        'membership_type' => $membership_type,
        'options'         => array(
          'limit'  => $page_size,
          'offset' => $offset
        )
      )
    );

    // do something with the results if we didn't error
    if ($results['is_error'] == 0) {
      foreach ($results['values'] as $value) {
        $memberships[] = $value;
      }
    }

    // Increment the offset by the page size
    $offset = $offset + $page_size;

  } while ($results['count'] >= $page_size); // Check if we still need to fetch results
  • I've used rowCount myself, but is there any difference between that and limit? – DaveFF Jun 3 '15 at 8:19
  • 1
    @DaveFF they both work, but options.limit appears to be the preferred parameter. I get feeling rowCount will be deprecated in a future api update. – Owen Kelly Jun 3 '15 at 8:55
7

Well here's an alternate way to try which returns the total result if you have no idea how much entries does the returned result will contain :

$count = civicrm_api('Membership', 'getcount', $params);

$params = array(
  'version' => 3,
  'sequential' => 1,
  'membership_type' => 1,
  'options' => array(
    'limit' => $count,
  ),
);

$result = civicrm_api('Membership', 'get', $params);
5

Adding options.limit to 0 returns the total result for the api which I feel is the best way to get more than 25 contacts :)

$params = array(
  'version' => 3,
  'sequential' => 1,
  'membership_type' => 1,
  'options' => array(
    'limit' => 0,
  ),
);

$result = civicrm_api('Membership', 'get', $params);

  • My only concern with this is when you have say 100,000 contacts you need to be able to parse hen in smaller chunks so as to not overwhelm your server. Thought for things like meme ship types where you likely have less than 100 this is very useful – Owen Kelly Jul 31 '15 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.