3

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. How to get response in JSON?

Any help would be appreciated.

Thanks

5
  • $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
    Commented Jul 23, 2015 at 8:14
  • @Xavier I have tried that too: module_load_include('php', 'civicrm', '../api/class.api'); $api = new civicrm_api3 (array ('server' => 'localhost/abc', 'api_key'=>'123456789', 'key'=>'123xyzABC')); if($api->Event->Get(array('sequential'=>1))) { // 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(); } exit; // */ It gives me: Unable to parse returned JSON
    – Parth Vora
    Commented Jul 23, 2015 at 9:01
  • Please edit your question or add comments when you have more information that could help resolve the problem. Answers should be reserved to describe a solution to the question asked.
    – samuelsov
    Commented Jul 23, 2015 at 16:06
  • @samuelsov , Sorry for that. I'm new here. I thought if I change my original question, how you guys know about that modifications? Is there any automated notification or something like that?
    – Parth Vora
    Commented Jul 24, 2015 at 9:13
  • No problem. Usually people add a bold "Edit" below the original text. If the question become too much of a back and forth discussion, then IRC or forum.civicrm.org is a better place.
    – samuelsov
    Commented Jul 24, 2015 at 19:52

2 Answers 2

2

Option one: API will be the best option

Please use this link in your instance http://localhost/abc/civicrm/api/explorer or http://d46.demo.civicrm.org/civicrm/api/explorer#explorer

you will get JSON output

4
  • Thanks for your reply, I have already seen those links. All I don't understand is the code. As per this link: wiki.civicrm.org/confluence/display/CRMDOC42/REST+interface First thing is to login: $test = simplexml_load_file("https://<your site>/sites/all/modules/civicrm/extern/rest.php?q=civicrm/login&name=Erik Hommel&pass=<password>&key=<site key>"); foreach ($test->children() as $kids) { $apikey = $kids->api_key; $sessionid = $kids->PHPSESSID; } Query: But what should be the value of "name" and "pass" parameters? Are those the credentials of a Drupal user with admin role (whic
    – Parth Vora
    Commented Jul 22, 2015 at 14:27
  • yes, name and pass are credentials of drupal user with admin right Commented Jul 22, 2015 at 16:15
  • @parthvora : Please first try getting the value in the browser (address bar) localhost/abc/sites/all/modules/civicrm/extern/…{"sequential":1,"api_key":999}/drupal_test/sites/all/modules/civicrm/extern/rest.php?entity=Contact&action=get&json={"sequential":1,"api_key":999}&api_key=999&key=123abc Commented Jul 23, 2015 at 8:30
  • I have tried below links directly into browser address bar as you suggested: localhost/abc/sites/all/modules/civicr ​m/extern/rest.php?entity=Contact&action=get&json={"sequential":1,"api_key":123456789}&a ​pi_key=123456789&key=123xyzABC localhost/abc/sites/all/modules/civicr ​m/extern/rest.php?entity=Contact&action=get&json={"sequential":1}&a ​pi_key=123456789&key=123xyzABC But I'm getting this: Unable to parse returned JSON
    – Parth Vora
    Commented Jul 23, 2015 at 9:26
2

It's probably easiest to use a wrapper around that call that hide the complexity.

You can copy api/class.api.php into your client server and use it as indicated in the example in the source:

 $api = new civicrm_api3 (array ('server' => 'http://example.org',
                                'api_key'=>'theusersecretkey',
                                'key'=>'thesitesecretkey'));

if $api->Contact->Get(array(
 'contact_type'=>'Individual',
 'last_name' => 'koot',
 '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();
}

should fetch from the remote civicrm server and let you do the needed with the data on your client server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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