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) »
  • api via REST
Pages: [1]

Author Topic: api via REST  (Read 1338 times)

lcdweb

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1620
  • Karma: 116
    • www.lcdservices.biz
  • CiviCRM version: many versions...
  • CMS version: Joomla/Drupal
  • MySQL version: 5.1+
  • PHP version: 5.2+
api via REST
March 08, 2012, 11:40:44 am
just a general note --

it looks like calling the API via REST doesn't work with v3. if you add version=3 to the params in the url, it treats it as if it's version 3 and it will process the request for create/update. but it errors on return because the function that constructs the return result doesn't conform to the api3 structure.

not a big deal to continue using api v2, except that you get the "this is deprecated..." warning all the time.
support CiviCRM through 'make it happen' initiatives!
http://civicrm.org/mih

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: api via REST
March 09, 2012, 12:17:30 am
does this apply to both the json and xml format?
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: api via REST
March 09, 2012, 01:19:08 am
Hi,

I'm using api v3 via rest. What's the url you are using & civi version)?

Definitely, DO NOT use the api v2, it will soon switch from deprecated to no removed
-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: api via REST
March 09, 2012, 01:26:27 am
I have use/tested the api v3 with REST too
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

lcdweb

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1620
  • Karma: 116
    • www.lcdservices.biz
  • CiviCRM version: many versions...
  • CMS version: Joomla/Drupal
  • MySQL version: 5.1+
  • PHP version: 5.2+
Re: api via REST
March 09, 2012, 03:45:54 am
this was just xml. json seems to be working.
if you make a call with xml and do not specify the version, the action is processed but it cannot return the response because of the appended [deprecated] element. i'll file a patch to fix that.

if I passed version=3 along with the string, it also could not return a response because the v3 array is much different in structure. I *think* the only issue is with the return response.
support CiviCRM through 'make it happen' initiatives!
http://civicrm.org/mih

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: api via REST
March 09, 2012, 04:29:43 am
Yep, there is a problem with the API and REST, which was there at API v2 but is even more obvious in API v3. Check  http://issues.civicrm.org/jira/browse/CRM-9250 Basically the way the xml function in Array.php deals

I am working on a solution using SimpleXML or XMLWriter, which will format the XML better. I will be working on this again the week after next. Are you happy to wait for that, or alternatively take over the issue?

Work in progress with SimpleXML:
Code: [Select]
<?php
ini_set
('display_errors', '1');

$test = array(
"is_error" => "0",
"values" => array(
"contact_id" => "5",
"first_name" => "Erik"));

$xmlResult = new SimpleXMLElement("<?xml version=\"1.0\"?>
<ResultSet xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></ResultSet>");
array_to_xml($test, $xmlResult);
echo "De string is : ".$xmlResult->asXML();
// function defination to convert array to xml
function array_to_xml($data, &$xmlResult) {
    foreach($data as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xmlResult->addChild("$key");
                array_to_xml($value, $subnode);
            }
            else{
                array_to_xml($value, $xmlResult);
            }
        }
        else {
            $xmlResult->addChild("$key","$value");
        }
    }
}

Work in progress with XMLWriter:
Code: [Select]
<?php
ini_set
('display_errors', '1');

$testArray = array(
"is_error" => 0,
"version" => 3,
"count" => 3,
"values" => array(
"1" => array(
"id" => "1",
"name" => "Non-profit",
"description" => "Any not-for-profit organization.",
"is_selectable" => "1",
"is_reserved" => "0",
"is_tagset" => "0",
"used_for" => "civicrm_contact"),
"2" => array(
"id" => "2",
"name" => "Company",
"description" => "For-profit organization.",
"is_selectable" => "1",
"is_reserved" => "0",
"is_tagset" => "0",
"used_for" => "civicrm_contact"),
"3" => array(
"id" => "3",
"name" => "Government Entity",
"description" => "Any governmental entity.",
"is_selectable" => "1",
"is_reserved" => "0",
"is_tagset" => "0",
"used_for" => "civicrm_contact")));

$writer = new XMLWriter();
$writer->openURI('php://output'); 
$writer->startDocument('1.0');
$level = false; 

/*
 * start the XML with the attributes for the ResultSet 
 */
$writer->startElement('ResultSet');
$writer->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
arrayToXML( $testArray, $writer );
$writer->endElement();
$writer->endDocument();
$writer->flush();


function 
arrayToXML ( $data, &$writer, $level = false ) {
foreach ( $data as $key => $value ) {
if ( !is_array( $value ) ) {
$writer->writeElement( $key, $value );
} else {
if ( $level ) {
$writer->endElement();
}
if (ctype_digit ( trim($key) ) ) {
$start = "Result";
} else {
$start = (string) $key;
}
$writer->startElement($start);
arrayToXML( $value, $writer );
$level = true;
}
}
}
« Last Edit: March 09, 2012, 04:31:28 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

lcdweb

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1620
  • Karma: 116
    • www.lcdservices.biz
  • CiviCRM version: many versions...
  • CMS version: Joomla/Drupal
  • MySQL version: 5.1+
  • PHP version: 5.2+
Re: api via REST
March 09, 2012, 08:00:43 am
yes, that's basically the issue.
i did a quick hack solution to just check if the result is v3 and if so, use the values element to construct the response, and that worked fine.

couple other things I noticed when using REST --
in Utils/REST.php around line 160 we set the header to js (for JSON). but that has already been set in extern/rest.php

also, when returning xml, i was occasionally getting the error that the xml declaration does not start at the beginning of the page -- i.e. there was a leading space. for some reason, in REST.php around line 168 when we set that declaration, I sometimes had the space inserted. not always. i though it might be some leftover debug code, but I cleaned up my file pretty well and it still occurred. maybe we can call that declaration directly in extern/rest.php immediately after the header is set? i think it makes more sense there anyway.

support CiviCRM through 'make it happen' initiatives!
http://civicrm.org/mih

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: api via REST
March 09, 2012, 03:38:59 pm
Agree, should be as close as the top as possible, dealing with headers is complicated enough.

My general opinion about xml is like hitting your finger with a hammer: it's so good when it stops! if you can, do the json thing.

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) »
  • api via REST

This forum was archived on 2017-11-26.