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) »
  • Rest API: some information does not return properly
Pages: [1]

Author Topic: Rest API: some information does not return properly  (Read 2027 times)

MignonneHalpern

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: Drupal 7.x
  • MySQL version: not sure
  • PHP version: not sure
Rest API: some information does not return properly
December 19, 2013, 11:54:44 am
Very weird issue.  I'm using the REST API to get participants based on the event_id.  For the most part it's working.  There are some events for which nothing gets returned, even though I KNOW, based on the api explorer and the data in CIVICRM, that there are participants in the event.  Here's an example:

I've cut and pasted the page from the api explorer, and you can see I get participants for event_id 144:  (i've cut off the actual user data at the bottom so personal info isn't revealed).


entity   |  action   |   debug  |   sequential  |   json
 
Generated codes for this api call
URL   
ajax query |REST query.
smarty   {crmAPI var='result' entity='Participant' action='get' event_id=144 sequential=1}
{foreach from=$result.values item=Participant}
  <li>{$Participant.some_field}</li>
{/foreach}
php   $params = array(
  'version' => 3,
  'event_id' => 144,
  'sequential' => 1,
);
$result = civicrm_api('Participant', 'get', $params);
javascript   CRM.api('Participant', 'get', {'event_id': 144, 'sequential': 1},
  {success: function(data) {
      cj.each(data, function(key, value) {// do something });
    }
  }
);
 {
   "is_error":0,
   "undefined_fields":["participant_test","event_id"],
   "version":3,
   "count":14,
   "values":[{
      "contact_id":"26956",
      "contact_type":"Individual",
      "contact_sub_type":"",

There are obviously 14 participants in this event.

However, this is the rest api call (which works for the vast majority of other events, so I know the code is right...):

$participantxml = simplexml_load_file("https://mysite.org/sites/all/modules/civicrm/extern/rest.php??entity=Participant&action=get&event_id=144&debug=1&json=1&key=$sitekey&api_key=$api_key");

                    print_r($participantxml);

This simply returns nothing at all.  If I replace event_id=144 with event_id=148 which is another event, almost identical to this one, just different participants, I get everything I expect when I do the print_r

I've tried getting rid of the debug statements, adding in a rowCount, just returning all events (this doesn't work at all), etc.  Any suggestions or ideas for what is wrong?  I'm guessing some kind of weird permissions issues, but don't know where to start.  How can I get some debug info on this if nothing at all is returned?



« Last Edit: January 03, 2014, 11:49:31 am by MignonneHalpern »

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: Rest API: some information does not return properly
December 22, 2013, 02:47:33 am
If you are going to debug (which makes sense as this is a weird problem) you would first probably take a look at CRM/Utils/REST.php. There is a function that outputs the result of the API, perhaps that tells you something. It should at least give you an idea if the problem is with the API or with the XML-formatting. (By the way, have you tried the same API call with JSON just to check if that is working? If so, you have hit a situation where the XML formatting does not handle correctly).
Does  that help as a start?
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: Rest API: some information does not return properly
December 22, 2013, 09:44:11 pm
@erik, seems that it's already json on the rest, so the xml if off the hook on that one.

There is very little different between the api explorer/ajax and the rest.

does the user that correspond to the api_key have the needed permissions? can you try to login with this account and try the api explorer?

do you log errors? can you check if there is something in it? you have hit a bug indeed, it should always return something, with is_error=1 if there is a problem, not silently die
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

MignonneHalpern

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: Drupal 7.x
  • MySQL version: not sure
  • PHP version: not sure
Re: Rest API: some information does not return properly
January 02, 2014, 06:22:19 am
when I use the api_explorer, it's the same user and api_key.  I don't have direct access to the Drupal/CiviCRM site but I can ask them to turn on debugging.  Or where would I turn on debugging?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Rest API: some information does not return properly
January 04, 2014, 02:01:44 pm
If the problem is specific to an event it's likely there is a character in the event description or some other field related to it that is breaking the json / xml
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: Rest API: some information does not return properly
January 05, 2014, 05:34:05 am
Ah, I know I think:
you define you want the result as json but try to decode it as an xml. obviously the result will be empty. Beside, you are using get for the api_key and key params (not good for security).

so for your example, instead of simplexml_load_file use json_decode(file_get_content ("http://"

if you client is in php, simply copy api/class.api.php  into your code and read it, you have an example on how to use it as a wrapper around api calls
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

MignonneHalpern

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: Drupal 7.x
  • MySQL version: not sure
  • PHP version: not sure
Re: Rest API: some information does not return properly
January 06, 2014, 01:08:30 pm
So, I think I know what's happening.  I was able to generate an error:
 parser error : StartTag: invalid element name
This ONLY happens in the case when the event includes a nonzero
participant_fee_level":["per person for xxx affiliate agencies"]

In this case, the error says:
Warning: simplexml_load_file() [function.simplexml-load-file]: <0>Per person for xxx affiliate agencies</0> in /home/virtual/site18/fst/var/www/html/cctomoodle/workingcivicrm.php on line 47

I know that it's not legal to give an XML tag a numeric value.  I don't know where the heck the XML schema is on the site to even check it. But I think this is the issue, no?  DOes anyone know how to fix this/work around it?  Should I report it as an error?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Rest API: some information does not return properly
January 06, 2014, 01:55:18 pm
The workaround is simple: don't use xml, use the json format.

It's likely faster and the json_decode json_encode is much more robust than the xml code.
X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

MignonneHalpern

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: Drupal 7.x
  • MySQL version: not sure
  • PHP version: not sure
Re: Rest API: some information does not return properly
January 06, 2014, 02:18:32 pm
I tried to do what you said and I can't get it work.  I literally cut and pasted the code from the api.class.php example and I get an "Unable to parse returned JSON" error! 
Here's my code: (with xxx's to protect the innocent). Also note that I had to put in an extra ')' at the end of the line if($api...) as it was not coded correctly in the example code.  Made me worry right off the bat!


$api = new civicrm_api3 (array ('server' => 'http://xxxx.org','api_key'=>$api_key,'key'=>$sitekey));
    if ($api->Contact->Get(array('contact_type'=>'Individual','return'=>'sort_name,current_employer'))) {
      // each key of the result array is an attribute of the api
      echo "\n contacts found " . $api->count;
      foreach ($api->values as $c) {
        echo "\n".$c->sort_name. " working for ". $c->current_employer;
      }
      // in theory, doesn't append
    } else {
      echo $api->errorMsg();
    }


Anyway, i get that unable to parse error every time.  No idea where to go now.  Can't use XML or JSON, any other way to do this?  It seems so simple yet many days and hours and hours later, I'm no further along

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Rest API: some information does not return properly
January 06, 2014, 09:07:10 pm

the xml / json generation is done here:

CRM/Utils/REST.php, function output

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Rest API: some information does not return properly
January 07, 2014, 12:43:58 am
can you paste the complete returned json? (from the api explorer) in the api explorer, we don't parse the result, so might be the reason. If too many sensitive data, can you validate the result to see if it's a correctly formatted one?

I can't understand were/why the json_encode goes wrong, never had any problem with it.

you can modify the api.class.php and echo the result too before the "unable to parse.." line.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

MignonneHalpern

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: Drupal 7.x
  • MySQL version: not sure
  • PHP version: not sure
Re: Rest API: some information does not return properly
January 07, 2014, 06:23:53 am
Here's the returned json from the query which I want to do - Participant in a particular event, just returning one row:
(again, changed names to xxx in a few places to protect the innocent)
the issue that I see is near "participant_fee_level":["Per person...     It seems the "[" is what causes the problem, at least for XML. I cannot understand why JSON doesn't work at all. in fact, it gets weirder.  Even though I am in the same file, with the same api key and site key, the error it gives me when using json is:
{"error_message":"Failed to authenticate key","is_error":1}
Why would the same keys work for XML but not for JSON? 


{"is_error":0,"version":3,"count":1,"id":4622,"values":{"4622":{"contact_id":"26956","contact_type":"Individual","contact_sub_type":"","sort_name":"Mxxxx, Jxxxx","display_name":"Jxxx Mxxx","event_id":"144","event_title":"E-learning Course: Introduction to XXXX","event_start_date":"2014-01-08 14:00:00","event_end_date":"2014-02-19 15:30:00","participant_id":"4622","participant_fee_level":["Per person for XXXX affiliate agencies"],"participant_fee_amount":"270.00","participant_fee_currency":"USD","event_type":"E-learning","participant_status_id":"1","participant_status":"Registered","participant_role_id":"1","participant_register_date":"2013-11-27 15:42:44","participant_source":"Online Event Registration: E-learning Course: Introduction to XXXX Practition","participant_note":"","participant_is_pay_later":"0","participant_is_test":"0","participant_registered_by_id":"","participant_discount_name":"","participant_campaign_id":"","civicrm_value_participation_details_5_id":"","custom_83":"","id":"4622"}}}

MignonneHalpern

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: Drupal 7.x
  • MySQL version: not sure
  • PHP version: not sure
Re: Rest API: some information does not return properly
January 07, 2014, 06:56:54 am
My bad on the last post, got the JSON working.  Sorry the sample code had the website as http instead of https! 
I stupidly just cut and pasted the code.
Would still think that the bracket  issue is a problem,  in that it kills XML, but I guess no one uses XML??  I GREATLY appreciate everyone's assistance on this.  thanks so much!

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Rest API: some information does not return properly
January 07, 2014, 12:39:32 pm
I think most people use json. From memory Erik H was interested in xml but apart from that I think that unless you have a strong reason for xml I would focus on json
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: Rest API: some information does not return properly
January 07, 2014, 11:01:14 pm
I think Erik discussed it with his shrink, they have solved the root problem and he's not interested anymore by xml ;)
-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) »
  • Rest API: some information does not return properly

This forum was archived on 2017-11-26.