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) »
  • API pull website/url values
Pages: [1]

Author Topic: API pull website/url values  (Read 1591 times)

wexzone

  • I post occasionally
  • **
  • Posts: 33
  • Karma: 0
API pull website/url values
February 24, 2011, 04:50:44 pm
I am not able to find how to pull website/url value for a contact.  I have several urls associated with each contact (ie. facebook, youtube, etc).

How do I get these?  I assumed it would be be: return.website, but that doesn't work.

Is there a list of variable names somewhere?


xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: API pull website/url values
February 24, 2011, 08:25:54 pm
Usually if you are using contact API the corresponding parameters are the same as the field name in the civicrm_contact table.

I suspect the keyword you have there is that you have multiple urls per contacts that would mean maybe you can use the location API?


Because location API is used for things like address (which can be many for 1 contacts), email, phone, im, etc etc.

wexzone

  • I post occasionally
  • **
  • Posts: 33
  • Karma: 0
Re: API pull website/url values
February 24, 2011, 08:58:22 pm
website values are stored in a table called civicrm_website

I've tried with location, but can't get anything to work.

Any other ideas?  I assume it is through another API.

Is there a list of variables and APIs somewhere?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API pull website/url values
February 25, 2011, 12:13:55 am
Hi,

You can use the API explorer http://civicrm.org/API_version_3 and the getField action.

Erik has just -or will very soon- push a new entity to fetch directly the websites in the API v3. He has split the location API into specific entities for phone, email website... Will be much easier.

X+
-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: API pull website/url values
February 25, 2011, 12:20:07 am
Starting writing this before Erik posted but I think it still applies if you are stuck with v2


I think the website BAO changed recently & got ahead of the api on this - we will need a website api soon-ish.

I expect however, that it will be implemented against v3 which will be in release 3.4 but if you're not using that you might need to just do an SQL query for now or implement your own

This is a function I use in v2 for get - you really only need to swap out option group  for the right entity & you have it working. This example doesn't take an array because of the instance I was using it in

Code: [Select]
function civicrm_helpers_get_optiongroup($optiongroup){
try {
 $params['name'] = $optiongroup ;
  _civicrm_initialize( true );
       require_once 'api/v2/utils.php';
    civicrm_verify_mandatory($params);
    require_once 'CRM/Core/BAO/OptionGroup.php';
    $optionGroupBAO = new CRM_Core_BAO_OptionGroup();
    $fields = array_keys($optionGroupBAO->fields());

    foreach ( $fields as $name) {
        if (array_key_exists($name, $params)) {
            $optionGroupBAO->$name = $params[$name];
        }
    }
   
    if ( $optionGroupBAO->find() ) {
      $results = array();
      while ( $optionGroupBAO->fetch() ) {
        _civicrm_object_to_array( $optionGroupBAO, $result );
        $results[$optionGroupBAO->id] = $result;
      }
      return civicrm_create_success($results,$params,$optionGroupBAO);
    } else {
      return civicrm_create_success(array(),$params,$optionGroupBAO);
    }

  } catch (PEAR_Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  } catch (Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  }
  }

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

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: API pull website/url values
February 25, 2011, 06:45:18 am
Quote from: wexzone on February 24, 2011, 08:58:22 pm
website values are stored in a table called civicrm_website

I've tried with location, but can't get anything to work.

Any other ideas?  I assume it is through another API.

Is there a list of variables and APIs somewhere?

If you really need, just do two calls right now.

Code: [Select]
<?php
// Use your contact API (search or get) to retrieve the list of contact_ids

// Then just use the DAO to retrieve the websites such as 

require_once('CRM/Core/DAO/Website.php');
$websites = new CRM_Core_DAO_Website();

$data = array();
foreach(
$contact_ids as $contact_id) {
     
$websites->query("SELECT url FROM civicrm_website WHERE contact_id = $contact_id");
     while(
$websites->fetch()) {
           
$data[$contact_id][] = $websites->url;
     }

}
?>


Just remember to escape your SQL statement, etc etc and do things proper.


Hope it helps,


Cheers!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API pull website/url values
February 25, 2011, 07:49:49 am
Hi,

Really, writting the API you miss is a couple of lines more and should be more future proof than going the SQL way (hopefully ;). Contributors welcome...

Otherwise, general sql trick: one single select ... with contact_id in implode (',', $arrayofid) is much faster than one select per id. Probably doesn't make a big difference when you got a handful, but I'm on a quest to diminish the number of sql requests generated ;)
-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: API pull website/url values
February 25, 2011, 01:15:35 pm
Xavier - I'm thinking if the rename (currently in the api branch) works then v3 api can be backported & writing new api becomes a lot more appealing. (writing new v2 api doesn't make sense right now & it's hard to write new v3 ones if you can't backport)
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

wexzone

  • I post occasionally
  • **
  • Posts: 33
  • Karma: 0
Re: API pull website/url values
February 25, 2011, 06:36:54 pm
I will try with the API first and see if I can get it to work.  Since I am an API newbie, it might take me a little while.  The SQL is appealing, mainly because I have tons of experience with that, but I prefer keeping things consistent, as it will be easier to upgrade when the time comes.

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: API pull website/url values
March 01, 2011, 12:11:12 am
For the record, I expect to have a website API finished by 16th March, would be great if you could take this over as this will make the delivery of Email/Phone earlier?
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API pull website/url values

This forum was archived on 2017-11-26.