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) »
  • Calling Api from an external server
Pages: [1]

Author Topic: Calling Api from an external server  (Read 1465 times)

ryeradio

  • I post frequently
  • ***
  • Posts: 185
  • Karma: 1
  • CiviCRM version: 4.2.1
  • CMS version: Drupal
  • MySQL version: MySQL5
  • PHP version: PHP5
Calling Api from an external server
August 21, 2012, 02:42:05 pm
We have an external server that needs to make calls to our civicrm database. I need to be able to write functions to check for an existing user, login to Civi and update a custom table. I read the API and I see how to create the php code. My question is where to I put this code, in my custom php folder?  and how does the external server call it?

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Calling Api from an external server
August 21, 2012, 03:24:03 pm
I think what you want is the REST API.

The steps would be:

1. Implement your code as an extension of the v3 API.
2. Use the REST API to access your CiviCRM install remotely.

I haven't done this (I'm just switching over to v3 myself), but it seems pretty well documented.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Calling Api from an external server
August 21, 2012, 03:51:44 pm
check out the api/class.api.php

you can copy it on your calling server, and do things like

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

 $api->Contact->Get(array ('email'=> 'email@tocheck.com');
if ($api->count) {
  print_r($api->values);
}

Not sure what custom table you want, custom field?

Anyway, it's pretty easy to use any of the existing api on a different server, either by building the url directly or by copying and using this class.api.php file

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

ryeradio

  • I post frequently
  • ***
  • Posts: 185
  • Karma: 1
  • CiviCRM version: 4.2.1
  • CMS version: Drupal
  • MySQL version: MySQL5
  • PHP version: PHP5
Re: Calling Api from an external server
August 27, 2012, 04:40:08 am
I am trying REST API and I am logging in with this URL https://www.lifelightstage.org/civicrm/extern/rest.php?q=civicrm/login&name=7forlife&pass=<pasword I created>&key=<my site key>&json=1

and I keep getting an access denied error. I tried logging in with the user name and password and that works. And I confirmed my site key is correct. Is there another reason why I would be getting the access denied page? I also changed the role to be administrator and that did not work either.

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Calling Api from an external server
August 27, 2012, 04:48:55 am
You have the URL wrong. It's:

https://www.lifelightstage.org/sites/all/modules/civicrm/extern/rest.php?q=civicrm/login&name=7forlife&pass=<pasword I created>&key=<my site key>&json=1
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Calling Api from an external server
August 27, 2012, 05:17:49 am
Hi,

if you are using a recent version of civi, use:

https://www.lifelightstage.org/sites/all/modules/civicrm/extern/rest.php?entity=contact&action=get&name=7forlife&api_key=.your contat key>&key=<my site key>&json=1


ie.
1) q=civicrm/entity/action is replaced by entity=entity&action=action
2) no need to do a login/pwd first anymore, call directly the entity/action you want
3) it uses the api column in the civicrm_contact to authenticate the user. Right now, you need to put it at the sql level directly


and as suggested in  this thread, if the server you are calling the api from is running php, copy api/class.api.php and use it as a wrapper around all that.

X+

P.S. For your info, we are likely to end up migrating to oAuth at one point (we don't have sponsors and so far too low in the priority list to tackle it)
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

ryeradio

  • I post frequently
  • ***
  • Posts: 185
  • Karma: 1
  • CiviCRM version: 4.2.1
  • CMS version: Drupal
  • MySQL version: MySQL5
  • PHP version: PHP5
Re: Calling Api from an external server
August 27, 2012, 07:53:52 am
Thank you for the responses. I am a little confused on the api key. Do we need to create a function to populate those? Should they be populated once a user creates a username/password?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Calling Api from an external server
August 27, 2012, 09:44:30 am
You need only one user/contact per external server contacting. I tend to use the same contact as needed for the cronjobs, and run manually update civicrm_contact set api_key = 'your key' where id = <this id>
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

ryeradio

  • I post frequently
  • ***
  • Posts: 185
  • Karma: 1
  • CiviCRM version: 4.2.1
  • CMS version: Drupal
  • MySQL version: MySQL5
  • PHP version: PHP5
Re: Calling Api from an external server
August 27, 2012, 04:21:27 pm
Can you just throw any ID in the api_id field? I added a guid id and I am getting an this error  {"error_message":"Invalid session or user api_key invalid","is_error":1}

ryeradio

  • I post frequently
  • ***
  • Posts: 185
  • Karma: 1
  • CiviCRM version: 4.2.1
  • CMS version: Drupal
  • MySQL version: MySQL5
  • PHP version: PHP5
Re: Calling Api from an external server
August 27, 2012, 05:16:16 pm
Forget the last post it works, thank you so much!

ryeradio

  • I post frequently
  • ***
  • Posts: 185
  • Karma: 1
  • CiviCRM version: 4.2.1
  • CMS version: Drupal
  • MySQL version: MySQL5
  • PHP version: PHP5
Re: Calling Api from an external server
August 30, 2012, 06:59:36 am
OK one more question on this:) Our external server would like to make calls based on user information. So for example a user will login and be able to see different things from the external call based on their username. Currently when I make the below call I am logging in using a username and password specifically for the external call. Can I make another url call right after the initial one to get user credentials? What I need is to be able to read and write user data. So my programmer is asking for a url to pass in username/password so they can be validated on the server side.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Calling Api from an external server
September 04, 2012, 08:19:39 am
I don't think the login action is supported anymore.

IMO, using login password to authenticate between servers shouldn't be the way of doing it, they are stuff like oAuth to deal with it.

and your external server shouldn't have to ask your users' password (bad security practice) nor to store them in clear (even more bad)

Not sure I fully understood your use case, but if you need a lot of users authenticating, I would suggest you to help us implementing an oAuth server in civi to deal with it the right way

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) »
  • Calling Api from an external server

This forum was archived on 2017-11-26.