4

API chaining works well in Javascript and PHP. For example, I can retrieve a contact's email addresses using this code generated by the API Explorer:

Php

$result = civicrm_api3('Contact', 'getsingle', array(
  'sequential' => 1,
  'return' => "display_name",
  'id' => 41194,
  'api.Email.get' => array('return' => "email"),
));

Javascript

CRM.api3('Contact', 'getsingle', {
  "sequential": 1,
  "return": "display_name",
  "id": 41194,
  "api.Email.get": {"return":"email"}
}).done(function(result) {
  // do something
});

However, the drush command generated by the API explorer doesn't work:

drush cvapi Contact.getsingle sequential=1 return="display_name" id=41194 api.Email.get={"return":"email"}
# Gives me the contact just fine, but no email

Is there any way to coax drush cvapi into processing chained api calls? If it's not currently possible, is anybody working on it?

In the meantime, how can I help to correct the API Explorer so it doesn't produce non-working drush commands?

  • Just to note that for the example you give, you can use ' return="display_name,email" ' without chaining, though that doesn't help the general case. – Aidan Apr 4 '16 at 10:19
  • Thanks @Aidan -- that will give me the primary email, but with chaining in JS or PHP I can retrieve multiple emails associated with a contact. – Noah Miller Apr 5 '16 at 7:57
7

Drush can read parameters from standard input in JSON format if you supply the argument --in=json. This is what we need for API chaining, because chaining uses nested parameters, and in contrast to a flat list of command line arguments (the default way to pass parameters to drush cvapi, JSON can handle nesting. So to retrieve the email addresses for contact ID 41194 along with that contact's display name, use:

echo '{
  "sequential": 1,
  "return": "display_name",
  "id": 41194,
  "api.Email.get": {"return":"email"}
}' | drush cvapi Contact.getsingle --in=json
  • This is good, with a couple caveats: the output is hard to parse, and it doesn't work on Joomla/WordPress. To resolve those issues, change drush cvapi to [cv api]( github.com/civicrm/cv). That notation works on more CMS's and supports better output formats (JSON, PHP serialize(), and sometimes CSV). – Tim Otten Jun 27 '17 at 1:05

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.