1

This question already has an answer here:

I'm new to CiViCRM. I want to fetch events which are created in CiViCRM which is hosted on another server. So basically I want to fetch CiViCRM data from one individual site to my site. I don't know that which are the ways to do it and which is the best one. But I think Rest option would be good if I'm not wrong. I'm not able to find any similar example which is near to my task. All I don't understand that how should I use my rest url with code:

  1. Option 1:

    $api = new civicrm_api3(array ('server' => 'http://localhost/abc',
        'api_key'=>'123456789',
        'key'=>'123xyzABC'));
    
  2. Option 2:

     $contactxml =       simplexml_load_file("http://localhost/abc/sites/all/modules/civicrm/extern /rest.php?entity=contact&action=get&key=<site    key>&api_key=$apikey&last_name=Koot");
     foreach ($contactxml->children() as $contact) {
       $display = $contact->display_name;
     }
    

Is there any other options. I want to go for option 1 if that is possible. And I want response in JSON if possible.

marked as duplicate by davejenx, Nicholai Jul 25 '15 at 12:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Jul 22 '15 at 16:33

This question came from our site for professional and enthusiast programmers.

2

The wiki page http://wiki.civicrm.org/confluence/display/CRMDOC/REST+interface pretty much describe what you could do with rest.

From the wiki page :

Before the REST interface can be used, two things have to be set up:

  • You need to know your site key which can be found in the civicrm.settings.php file labelled CIVICRM_SITE_KEY.
  • You will need to enter an API key for every user that will be allowed to use the REST interface. (See below)

Then you will have to call an url like :

https://www.example.org/path/to/civi/codebase/civicrm/extern/rest.php?entity=event&action=get&key=your_site_key&api_key=the_user_api_key&json=1
  • Option json=1 will give you result in json format instead of xml
  • You may need to use &options[limit]=xxx if you want more than 25 results

Every options available in the api should be available in REST but need to be converted in the proper URL syntax. The wiki page give you some good examples of standard api call vs REST api call.

  • Thanks for your reply, I have already seen that link. All I don't understand is the code. As per this link: wiki.civicrm.org/confluence/display/CRMDOC42/REST+interface – Parth Vora Jul 22 '15 at 14:33
  • The link you give is for CiviCRM <= 4.2, please use the link i gave for a more recent version. What code don't you understand exactly ? – samuelsov Jul 22 '15 at 18:26
  • $test = simplexml_load_file('localhost/xyz/sites/all/modules/contrib/civicrm/extern/rest.php/…}&api_key=123456789&key=123xyzABC'); print_r($test);exit; For above code I'm getting this: SimpleXMLElement Object ( [Result] => SimpleXMLElement Object ( [error_message] => Failed to authenticate key [is_error] => 1 ) ) both keys are also correct. What I'm doing wrong here? – Parth Vora Jul 23 '15 at 8:12
  • 2
    The error 'Failed to authenticate key' indicates that the 'key' parameter in your request does not match the CIVICRM_SITE_KEY in the remote site's civicrm.settings.php file. – davejenx Jul 23 '15 at 9:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.