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) »
  • Chained / nested API calls over HTTP - does it work?
Pages: [1]

Author Topic: Chained / nested API calls over HTTP - does it work?  (Read 547 times)

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Chained / nested API calls over HTTP - does it work?
May 24, 2014, 06:24:10 pm
From my read of the API and docs, I expected that we'd be able to use chained API calls over the HTTP interface (extern/rest.php). I spent a while with this yesterday and didn't see any way to make it function, eventually concluding that it doesn't seem to work yet.

Here's some sample code I'd expect to work (from api/v3/examples/Contact/ChainTwoWebsites.php)

I was testing this against a 4.3 site and haven't had a chance yet to confirm for 4.4. I see that api/api.php has been rewritten a fair bit in 4.5. Would be interested if any other devs who use the extern/rest.php interface can confirm this as a bug for 4.4 or 4.5 branches.

Code: [Select]
#!/usr/bin/php
<?php

// Use class.api.php from CiviCRM
include('/path/to/site/sites/all/modules/civicrm/api/class.api.php');
$api_params = array(
  
'server' => 'http://civicrm.local',
  
'api_key' => 'USERKEY',
  
'key' => 'SITEKEY',
);

$api = new civicrm_api3($api_params);

$params = array(
  
'first_name' => 'abc3',
  
'last_name' => 'xyz3',
  
'contact_type' => 'Individual',
  
'email' => 'man3@yahoo.com',
  
'api.contribution.create' => array(
      
'receive_date' => '2010-01-01',
      
'total_amount' => '100',
      
'financial_type_id' => 1,
      
'payment_instrument_id' => 1,
      
'non_deductible_amount' => '10',
      
'fee_amount' => '50',
      
'net_amount' => '90',
      
'trxn_id' => 15345,
      
'invoice_id' => 67990,
      
'source' => 'SSF',
      
'contribution_status_id' => 1,
    ),
  
'api.website.create' => array(
      
'url' => 'http://civicrm.org',
    ),
  
'api.website.create.2' => array(
      
'url' => 'http://chained.org',
    ),
);

try{
  
$result = $api->Contact->create($params);
  
print_r($api);
}
catch (
CiviCRM_API3_Exception $e) {
  
// handle error here
  
$errorMessage = $e->getMessage();
  
$errorCode = $e->getErrorCode();
  
$errorData = $e->getExtraParams();
  return array(
'error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
}

I suspect that handling passing in arrays would be easier done if we submit data to the interface as JSON rather than keyed POST values?

Will come back to this but didn't look any further yet - it's the weekend, and implementing what I needed without chained calls took me a third of the time it did to discover that chained calls weren't working :)

Do the API test cases get tested over the HTTP interface or just from within PHP?
@xurizaemon ● www.fuzion.co.nz

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Chained / nested API calls over HTTP - does it work?
May 26, 2014, 12:11:32 am
I am not sure, but I think the chained API calls are only tested from within PHP
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Chained / nested API calls over HTTP - does it work?
May 26, 2014, 12:29:57 am
I had a modified class.api that handled json_encoding.

I'm not sure why I didn't PR it, I'm swamped with the european election results (they suck) but will try to find it and PR or at least send to you for testing.

@Erik, definitely works over rest, I used from node.js
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Chained / nested API calls over HTTP - does it work?
May 26, 2014, 12:42:37 am
Thanks X!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Chained / nested API calls over HTTP - does it work?

This forum was archived on 2017-11-26.