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) »
  • Contact API 3.3
Pages: [1]

Author Topic: Contact API 3.3  (Read 861 times)

rtacconi

  • I’m new here
  • *
  • Posts: 1
  • Karma: 0
  • CiviCRM version: 3.3
  • CMS version: Druapl 6
  • MySQL version: 5.5
  • PHP version: 5.2.3
Contact API 3.3
August 04, 2011, 01:28:59 am
Hi I am having a huge amount of issues when sending user information updates from one web site to another one. They are both Drupal site, but one has Civicrm. I tried to use the REST interface but I realized that it is not a REST interface since it is using PHP Session, the developer should learn what REST is, then implement it. The other problem is the lack of libraries to use REST in PHP. In the end I am sending the data using my custom HTTP calls. When I receive the data as HTTP parameters ($_REQUEST) I create an array this way:

function get_profile() {
  return array(
    'contact_type' => 'Individual', // to be checked
    'email' => $_REQUEST['email'],
    'title' => $_REQUEST['title'],
    'first_name' => $_REQUEST['first_name'],
    'surname' => $_REQUEST['surname'],
    'display_name' => $_REQUEST['display_name'],
    'telephone' => $_REQUEST['telephone'],
    'address_1' => $_REQUEST['address_1'],
    'address_2' => $_REQUEST['address_2'],
    'town_city' => $_REQUEST['town_city'],
    'province' => $_REQUEST['province'],
    'postcode' => $_REQUEST['postcode'],
    'website' => $_REQUEST['website'],
    'discipline' => $_REQUEST['discipline'],
    'other_discipline' => $_REQUEST['other_discipline'],
    'sex' => $_REQUEST['sex'],
    'disabled' => $_REQUEST['disabled'],
    'education_establishment' => $_REQUEST['education_establishment'],
    'ethnicity' => $_REQUEST['ethnicity'],
    'other_ethnicity' => $_REQUEST['other_ethnicity'],
    'other_ethnicity_non_white' => $_REQUEST['other_ethnicity_non_white']
  );
}

Than I search for a contact using the email address, if the contact is not there I create it

civicrm_contact_create(get_profile());

or I update it:

civicrm_contact_update($contact); // when update add contact id to the array

The problem is I get this error:

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/opt/local/apache2/htdocs/craftcentral-crm/sites/all/modules/civicrm/CRM/Core/BAO/Website.php</b> on line <b>78</b><br />[Debug] 18.

The CRM code is the following:

     static function create( &$params, $contactID, $skipDelete ) {
         if ( empty( $params ) ) {
             return false;
         }
         
         $ids = self::allWebsites( $contactID );
         
         foreach ( $params as $key => $values ) {
             $websiteId = CRM_Utils_Array::value( 'id', $values );
             if ( $websiteId ) {
                 if ( array_key_exists( $websiteId, $ids ) ) {
                     unset( $ids[$websiteId] );
                 } else {
                     unset( $values['id'] );
                 }
             }

Do you have an idea of what kind of array I should send? I am actually sending an array. What that function is doing. Websites? The code is very inexpressive and not really easy to understand what is achieving. Please advise.

UPDATE: it seems that the create and update call is working although there is this warning
« Last Edit: August 04, 2011, 02:09:05 am by rtacconi »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Contact API 3.3
August 04, 2011, 03:25:30 am
Quote from: rtacconi on August 04, 2011, 01:28:59 am
Hi I am having a huge amount of issues when sending user information updates from one web site to another one. They are both Drupal site, but one has Civicrm. I tried to use the REST interface but I realized that it is not a REST interface since it is using PHP Session, the developer should learn what REST is, then implement it.

Agree, it shouldn't use session and we think about moving to token and oAuth. Want to work on it?

(btw, they are plenty of other reasons the REST interface is RESTish but not real REST)

Quote

The other problem is the lack of libraries to use REST in PHP.


I'm working on a client class to be able to call REST(ish) resources as easily as calling local ones. Aim: civicon 22th of august.

This being said, that's not complex to write a few lines of PHP to have your own minimal client.

Quote

civicrm_contact_create(get_profile());


This syntax if for API v2. I urge you to move to api v3 for new code.
(so the call is
civicrm_api('contact','create', get_profile);

http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs


Quote
Do you have an idea of what kind of array I should send? I am actually sending an array. What that function is doing. Websites? The code is very inexpressive and not really easy to understand what is achieving. Please advise.


I would recommend you to use the api explorer to see what fields are available
http://drupal.demo.civicrm.org/civicrm/ajax/doc#/civicrm/ajax/rest?json=1&debug=1&version=3&entity=contact&action=getfields

and to call directly the website.create api for the website.

http://drupal.demo.civicrm.org/civicrm/ajax/doc#/civicrm/ajax/rest?json=1&debug=1&version=3&entity=Website&action=get
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Contact API 3.3
August 04, 2011, 03:29:06 am
Didn't notice you are still on 3.3. The API has been greatly improved since then. Try to migrate to the latest 3.4 or backport the api (eileen has written instructions on how to do it).

Out of curiosity, how do you handle authentication with your interface?

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) »
  • Contact API 3.3

This forum was archived on 2017-11-26.