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 (Moderator: Donald Lobo) »
  • Civicrm api : getting data by passing an array
Pages: [1]

Author Topic: Civicrm api : getting data by passing an array  (Read 843 times)

valfr35

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.x
  • CMS version: Drupal
  • MySQL version: 5.1
  • PHP version: 5.3
Civicrm api : getting data by passing an array
March 04, 2014, 03:38:50 am
Hi everyone,

With api v3, it's possible to get a list of contact by passing an array of contact_id

example:
/civicrm/ajax/rest?entity=Contact&action=get&debug=1&sequential=1&json=1&contact_id[]=1,2

Unfortunately, it may not possible to do the same for membership, contribution, etc...

As I'm not experienced on civicrm api, i search for a good practice to add this "bulk way of getting data" with entities other than contact.

Thanks !

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Civicrm api : getting data by passing an array
March 04, 2014, 03:49:10 am
Yes, - here is the test for retrieving data using an array of contact ids

https://github.com/civicrm/civicrm-core/blob/4.4/tests/phpunit/api/v3/ContactTest.php#L462

note that

$this->callAPISuccess()
is the same as civicrm_api3() - but with a check that it succeeded
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: Civicrm api : getting data by passing an array
March 04, 2014, 04:01:00 am
If you want to use it over rest/ajax, you'll need to json encode and put all the params into a post json param, eg.

json={"entity:"contact","action":"get","id"={"in":[1,2]}...} and it should work
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

valfr35

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.x
  • CMS version: Drupal
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Civicrm api : getting data by passing an array
March 04, 2014, 06:23:33 am
Thanks, it's working with contact entity,

But how can i do the same with others entities ?
For example, if i want to retrieve 2 memberships by passing an array of 2 ids :

i have unsuccessfully tried this:
json={"entity:"membership","action":"get","membership_id"={"in":[1,2]}...}
or
/civicrm/ajax/rest?entity=membership&action=get&debug=1&sequential=1&json=1&membership_id[]=1,2



Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Civicrm api : getting data by passing an array
March 05, 2014, 01:59:50 pm
looking at the code it appears it *should* work - although we recommend you use 'id' not 'membership_id'
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

valfr35

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.x
  • CMS version: Drupal
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Civicrm api : getting data by passing an array
March 14, 2014, 07:42:44 am
It's work !

... I have made a mistake with id.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Civicrm api : getting data by passing an array

This forum was archived on 2017-11-26.