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 - Update contact.
Pages: [1]

Author Topic: Rest API - Update contact.  (Read 1546 times)

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Rest API - Update contact.
November 08, 2012, 03:38:58 pm

Hello,

I want an external server to update a record via the REST API.

It will need to update 1 or more custom fields and the external system will know which CiviCRM contact ID it needs to update.

So basically, I'm looking for a way to build a url that in one go, can update a custom field of a specified contact ID.

Thanks!

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 - Update contact.
November 08, 2012, 04:03:24 pm
Have you tried the custom field api? They are examples or you can use the api explorer to test
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Rest API - Update contact.
November 08, 2012, 05:51:14 pm
don't you just need the rest equivalent of

civicrm_api('contact', 'create', array('version' => 3, 'id' => $contact_id, 'custom_1' => 'newValue', 'custom_34' => 'another val'));

Assume you know the api explorere?
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

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Rest API - Update contact.
November 08, 2012, 06:43:09 pm

Thanks Xavier + Eileen

Also, is there anyway to authenticate + update in one go?

The reason that I ask, is that I only get one chance to form a url that the external server can post. It's an external commercial mobile phone data collecting service you see.

What I'm doing is providing a smart group that a Drupal View exports via JSON on a certain page...the mobile data company server picks it up and imports the data as a list of people to canvas..with the CiviCRM contact ID as part of that data.

Then once the questions are answered, the idea is that the mobile data company's server will simply post back the update using REST - but as I said, it really needs to be done in one hit.

Thanks!

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 - Update contact.
November 08, 2012, 11:59:28 pm
You can try if you can chain the login call and the update call in one call (see the section on chaining in the developer guide http://book.civicrm.org/developer/current/techniques/api/). But I am note sure if you will be able too, I guess I would expect the login to not accept that. But as I am not sure , give it a try!
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 - Update contact.
November 09, 2012, 01:39:29 am
Ok, if even Erik missed that, I seriously screwed up on the documentation front:
you don't need to login anymore, if you have api_key and key as param on any action it works.

as for chaining, this is a brand new addition for 4.2.5: you can json encode the param, including the api.entity.action, and shove it into json= param.

As for the others, you need to use http post, not get if you call an api that modifies the state (eg. anything else than get)


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

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Rest API - Update contact.
November 09, 2012, 02:12:09 am
Quote from: xavier on November 09, 2012, 01:39:29 am
Ok, if even Erik missed that, I seriously screwed up on the documentation front:
you don't need to login anymore, if you have api_key and key as param on any action it works.

as for chaining, this is a brand new addition for 4.2.5: you can json encode the param, including the api.entity.action, and shove it into json= param.

As for the others, you need to use http post, not get if you call an api that modifies the state (eg. anything else than get)

Great. I'm just missing something here...I have the site key defined in civicrm_settings what is this other key?

I just need one example of using those 2 keys while updating a contact and then I'm set.

The remote server can post the data, that's not a problem.

Thanks

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 - Update contact.
November 09, 2012, 02:31:10 am
example in node
https://github.com/tttp/civi-charlatan
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Rest API - Update contact.
November 09, 2012, 03:27:59 am

Ah I can't json encode stuff...least I don't think I can.

I'll repeat what's going on.

I'm trying to get a remote third party server not under my control, that gives me a chance via it's control panel, to send data back collected data via a url.

It will also allow be to specify what collected data is posted as what variable.

Can the key(s) or all the authentication stuff be posted in as a variable?

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 - Update contact.
November 09, 2012, 03:44:47 am
You know you can try directly from your browser?
Will be faster for you to try, as eileen suggested, you can use the api explorer to generate the url. Do that and you will see you it's then just a matter of replacing the keys with your own values

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

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Rest API - Update contact.
November 09, 2012, 04:28:36 am
Quote from: xavier on November 09, 2012, 03:44:47 am
You know you can try directly from your browser?
Will be faster for you to try, as eileen suggested, you can use the api explorer to generate the url. Do that and you will see you it's then just a matter of replacing the keys with your own values

X+

https://www.mysite.com/sites/all/modules/civicrm//extern/rest.php?json=1&debug=1&version=3&entity=Contact&action=update&api_key={yoursitekey}&key={yourkey}

How / Where do I get {yourkey} from? ... this is the bit that I'm missing, sorry if I've missed it in the docs.

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Rest API - Update contact.
November 09, 2012, 09:10:40 am

OK. I've found an explanation in the docs and I've updated the api_key field against that user in the civicrm_contacts table, but it still doesn't seem to have gotten me anywhere.

{
   "error_message":"Invalid session or user api_key invalid",
   "is_error":1
}

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Rest API - Update contact.
November 10, 2012, 04:50:18 am

OK....the api explorer has it the wrong way around! grr!

It may have been fixed on a later version. If it has then fine, if I find that it hasn't then I'll have it booked as a bug.

Hopefully I'll be OK from here...

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 - Update contact.
November 10, 2012, 05:52:49 am
Not sure I understood what's the wrong way around.

Can you check on the latest version and add an issue if it's not the case?

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

SweetTooth

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 1
  • CiviCRM version: 4.1
  • CMS version: Drupal 6.20
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Rest API - Update contact.
November 10, 2012, 06:21:49 am

Xavier,

Actually thinking about it, it's just a misunderstanding on my part. Everything is all correct, though perhaps {key} should be described as {site_key}.

I've now got this updating the record.

However, it would be great to know how to chain commands using REST as well.

Many thanks!


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Rest API - Update contact.

This forum was archived on 2017-11-26.