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) »
  • numb as a noob
Pages: [1]

Author Topic: numb as a noob  (Read 1090 times)

pike67

  • I post occasionally
  • **
  • Posts: 55
  • Karma: 0
  • CMS version: drupal7
numb as a noob
July 12, 2012, 09:19:05 am
Hi

I'm struggling to get some sensible data out of the api. I think my problem is I'm missing documentation and it somehow the logic doesnt come across naturally either ;-)  How does that 'params' syntax work ? What does it generally expect ? What are the exceptions ? Is this documentated somewhere ?

I want a list of individuals from within a group, sorted, with some info on related organisations for that contact.
I managed to get this far (this works:)

Code: [Select]

civicrm_initialize(); /* i'm inside a drupal module */
$params = array(
'group_id' => $mygroup,
'version' => 3,
'api.contact.get' => 1
);

$results = civicrm_api( 'group_contact','get',$params );
if (!$results['is_error']) {
foreach ($results['values'] as $result) {
if (!$result['api.contact.get']['is_error']) {

if ($result['api.contact.get']['values'][0]['contact_type'] == 'Individual') {
$contacts[] = $result['api.contact.get']['values'][0];
}
usort($contacts,'my_sort_func');

} else {
//print "api.error 2";
}
}
} else {
//print "api.error 1";
}
return $contacts;

Boy, that looks ugly. Can I filter for individuals in the first place, instead of looping and filtering them in php ? Can I perhaps do the sorting via the $params ?

I still dont have any related organisation information ( I have the 'current_employer', but only if the type of relation is 'employee', which is not what I want). I *think* I cant get that information by chaining, because I *think* I can only chain one level deep. But I dont *know*. In the $params above, what does 'api.contact.get' => 1 mean ? If I write '2' there, what happens ?

So suppose I have an array of contact_ids, can i get related organisations for all those ids in one fetch ? something like .. filtering all relations with an array ? http://svn.civicrm.org/civicrm/trunk/api/v3/examples/Relationship/filterIsCurrent.php
Or do I have to do a separate api call for each contact ? And even if so,  what call would get me related contacts ?

I solemly pledge, once I understand all of this, I will write it down and post it somewhere for future noobs :-D

*-pike
« Last Edit: July 12, 2012, 09:23:01 am by pike67 »

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: numb as a noob
July 12, 2012, 11:58:53 am
Check out the API Explorer at civicrm/api/explorer on your site. I think it will help you out a lot.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

pike67

  • I post occasionally
  • **
  • Posts: 55
  • Karma: 0
  • CMS version: drupal7
Re: numb as a noob
July 13, 2012, 04:31:50 am
Quote from: Hershel on July 12, 2012, 11:58:53 am
Check out the API Explorer at civicrm/api/explorer on your site. I think it will help you out a lot.

Thanks, I did. I can pass entity and action, but how do I pass $params ?
How can I do nested (chained) queries in the api explorer ?
And could I pass $filters in the api explorer ?

curious,
*-pike
« Last Edit: July 13, 2012, 04:34:34 am by pike67 »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: numb as a noob
July 13, 2012, 05:03:08 am
by modifying the string url (next to the GO button)

When you load it the first time, it's configured to show an example of contact search filtered and return param.

It generates a php example, that should be pretty strait-forward to go from one php version to the url and vice versa.

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

pike67

  • I post occasionally
  • **
  • Posts: 55
  • Karma: 0
  • CMS version: drupal7
Re: numb as a noob
July 13, 2012, 06:51:59 am
Quote from: xavier on July 13, 2012, 05:03:08 am
by modifying the string url (next to the GO button)
When you load it the first time, it's configured to show an example of contact search filtered and return param.
It generates a php example, that should be pretty strait-forward to go from one php version to the url and vice versa.

OK .. I dont see that. I see an url
civicrm/ajax/rest?json=1&debug=on&entity=Contact&action=get&sequential=1&return=display_name,email,phone

it never changes, no matter what i select in the popups or if i click 'go'. I dont see a php example either. if i go to /civicrm/api/doc, which looks promising, all i get is javascript warnings, and the links dont work.

Hm, ok, let me try that from a windows box later :-/

thanks,
*-pike

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: numb as a noob
July 13, 2012, 07:38:09 am
Quote from: pike67 on July 13, 2012, 06:51:59 am
Quote from: xavier on July 13, 2012, 05:03:08 am
by modifying the string url (next to the GO button)
When you load it the first time, it's configured to show an example of contact search filtered and return param.
It generates a php example, that should be pretty strait-forward to go from one php version to the url and vice versa.

OK .. I dont see that. I see an url
civicrm/ajax/rest?json=1&debug=on&entity=Contact&action=get&sequential=1&return=display_name,email,phone

it never changes, no matter what i select in the popups or if i click 'go'. I dont see a php example either. if i go to /civicrm/api/doc, which looks promising, all i get is javascript warnings, and the links dont work.

Hm, ok, let me try that from a windows box later :-/

thanks,
*-pike

Can you check on demo?
you are supposed to see that, or something close to it:
http://civicrm.org/API_version_3
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

pike67

  • I post occasionally
  • **
  • Posts: 55
  • Karma: 0
  • CMS version: drupal7
Re: numb as a noob
July 24, 2012, 05:39:34 am
Quote from: xavier on July 13, 2012, 07:38:09 am
Quote from: pike67 on July 13, 2012, 06:51:59 am

...all i get is javascript warnings, and the links dont work.


Can you check on demo?
you are supposed to see that, or something close to it:
http://civicrm.org/API_version_3

Ah, I see it from a more modern mac. I'll go back to the older mac and see what goes wrong there, now that I know what it should look like.

I still dont think I can do nested queries there, like

Code: [Select]
../civicrm/ajax/rest?json=1&debug=1&version=3&entity=GroupContact&group_id=3&api.contact.get=1&return=display_name&action=get
returns

Code: [Select]
"undefined_fields":["group_id","api_contact_get","status"],
.. and i do get results of group 3, but not their display name. It works in php though. Strange.

thanks,
*-pike

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: numb as a noob
July 24, 2012, 05:59:34 am
I don't think nested queries are supported in JSON.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: numb as a noob
July 24, 2012, 11:23:38 am
Hi,

If the goal is to get a list of contact, you should use the api contact.get with group (or group_id, not sure by heart) param set.

/civicrm/ajax/rest?json=1&debug=1&version=3&entity=Contact&group=3&return=display_name&action=get

This should do it.

Avoid chaining api calls, the performance is obviously not going to be super good.

There is a way to call with chaining, ( /civicrm/ajax/json?{'entity':'contact' ... if I recall, but that's still experimental/will change)

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • numb as a noob

This forum was archived on 2017-11-26.