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 (Moderator: Donald Lobo) »
  • Cant get any example snippets to work - Parse error: syntax errors
Pages: [1]

Author Topic: Cant get any example snippets to work - Parse error: syntax errors  (Read 1338 times)

psampat

  • Guest
Cant get any example snippets to work - Parse error: syntax errors
March 23, 2009, 04:18:02 am
hi everyone-

im new to php and to the civicrm api's.  i'm trying to figure out how to access them, and i've tried and failed for many hours now to get any sample snippet to work.  I am posting these through php input on a drupal page.

For example the last one I tried is http://wiki.civicrm.org/confluence/display/CRMDOC/Using+CiviCRM+APIs+-+Code+Snippets#UsingCiviCRMAPIs-CodeSnippets-DisplayasortedlistofgroupmembersinaDrupalblockusingcivicrmcontactsearch


Code: [Select]
<?php
require_once('./sites/default/civicrm.settings.php');
global 
$civicrm_root;
civicrm_initialize( );

require_once(
'CRM/Core/Config.php');
$config =& CRM_Core_Config::singleton( );

require_once (
'api/v2/Contact.php');

$myGroup = 'Board Member';

$results = getGroupMembers($myGroup);
if (
$results) {
   echo 
'<h3>' . $myGroup . '</h3>';
   foreach (
$results[0] as $myContact) {
       
$string = 'ID: ' . $myContact[CRM:'contact_id'] . ' ' .
            
$myContact[CRM:'sort_name'] . ', ' . $myContact[CRM:'email'] . ', Phone: ' .
            
$myContact[CRM:'phone'];
       echo 
$string . '<br />---<br />';
   }
}
else {
   echo 
'No group found with that name.';
}

function 
getGroupMembers($group_title) {
 
// Check if CiviCRM is installed here.
 
if (!module_exists('civicrm')) return false;

 
// Initialization call is required to use CiviCRM APIs.
 
civicrm_initialize( );

 
// Pass group 'title' to civicrm_groups_get to retrieve the group_id.
$groupGet = array('title' => $group_title)
 
$groups =& civicrm_groups_get( $groupGet );

 
// Pass 'group' => group.id into search API to find members of that group
 // Only one group with that title, so we can reference
 // first result in returned array from get_groups.
 // Also pass on the required return properties and sort by criteria in params
 
$params = array('group' => array($groups[0]['id'] => 1)
                          'return.sort_name' => 1,
                                  
'return.email' => 1,
                                  
'return.phone' => 1
                          
'sort' => array('sort_name' => 'ASC') );

 
$contacts =& civicrm_contact_search( $params );
 if (!
$contacts) return false;
 return 
$contacts;
}
?>

Which returns:

Parse error: syntax error, unexpected ':', expecting ']' in /home/content/z/i/m/zimpano/html/drupal/includes/common.inc(1547) : eval()'d code on line 21

Can you help me?  I can also post all the other sample snippets i've tried which all give different types of syntax, parse, unexpected T_Variable, etc errors.  I know it has to be something in lines of code in the beginning.  Thanks for your patience with the newbie!

I have PHP 5.1, MYSQL 4.0.27, 4.1, 5.0, CiviCRM 2.1beta5, Drupal 6.4

Thanks so much for your help!

Sunil

  • I post frequently
  • ***
  • Posts: 131
  • Karma: 23
  • The community around a product more important than the product itself?
    • CiviCRM
Re: Cant get any example snippets to work - Parse error: syntax errors
March 23, 2009, 05:17:23 am
Hi,
Will you  try this

Code: [Select]
<?php

require_once '../civicrm.config.php';
require_once 
'CRM/Core/Config.php';
require_once 
'CRM/Core/Error.php';
$config = CRM_Core_Config::singleton( );

require_once 
'api/v2/Contact.php';
require_once 
'api/v2/Group.php';

$myGroup = 'Newsletter Subscribers';

$results = getGroupMembers($myGroup);

if (
$results ) {
    echo 
'<h3>' . $myGroup . '</h3>';
    foreach ( 
$results as $keys => $myContact ) {
        
$string = 'ID: ' . $myContact['contact_id'] . ' ' .
            
$myContact['sort_name'] . ', ' . $myContact['email'] . ', Phone: ' .
            
$myContact['phone'];
        echo 
$string . '<br />---<br />';
   }
}
else {
   echo 
'No group found with that name.';
}

function 
getGroupMembers($group_title) {

    
// Pass group 'title' to civicrm_groups_get to retrieve the group_id.
    
$groupGet = array( 'title' => $group_title);
    
$groups =& civicrm_group_get( $groupGet );
    
    
$groupID =  array_keys($groups);

    
// Pass 'group' => group.id into search API to find members of that group
    // Only one group with that title, so we can reference
    // first result in returned array from get_groups.
    // Also pass on the required return properties and sort by criteria in params
    
$params = array(
                    
'group'            => array( $groupID[0] => 1),
                    
'return.sort_name' => 1,
                    
'return.email'     => 1,
                    
'return.phone'     => 1
                    
);
 
    
$contacts =& civicrm_contact_search( $params );
    return 
$contacts;
}

Quote
http://wiki.civicrm.org/confluence/display/CRMDOC/Using+CiviCRM+APIs+-+Code+Snippets#UsingCiviCRMAPIs-CodeSnippets-DisplayasortedlistofgroupmembersinaDrupalblockusingcivicrmcontactsearch
There is some sort for error in given example (e.g. somewhere comma is missing in the code, sort key not working etc) we will correct it.

Sunil
The community around a product more important than the product itself?

psampat

  • Guest
Re: Cant get any example snippets to work - Parse error: syntax errors
March 23, 2009, 05:34:24 am
Okay, so I figured you wanted me to actually enter the code like this (fixing for civicrm.config.php file location) and leaving in the first three lines.

Code: [Select]
<?php


require_once('./sites/default/civicrm.settings.php');
global 
$civicrm_root;
civicrm_initialize( );

require_once 
'civicrm.config.php';
require_once 
'CRM/Core/Config.php';
require_once 
'CRM/Core/Error.php';
$config = CRM_Core_Config::singleton( );

require_once 
'api/v2/Contact.php';
require_once 
'api/v2/Group.php';

$myGroup = 'Newsletter Subscribers';

$results = getGroupMembers($myGroup);

if (
$results ) {
    echo 
'<h3>' . $myGroup . '</h3>';
    foreach ( 
$results as $keys => $myContact ) {
        
$string = 'ID: ' . $myContact['contact_id'] . ' ' .
            
$myContact['sort_name'] . ', ' . $myContact['email'] . ', Phone: ' .
            
$myContact['phone'];
        echo 
$string . '<br />---<br />';
   }
}
else {
   echo 
'No group found with that name.';
}

function 
getGroupMembers($group_title) {

    
// Pass group 'title' to civicrm_groups_get to retrieve the group_id.
    
$groupGet = array( 'title' => $group_title);
    
$groups =& civicrm_group_get( $groupGet );
    
    
$groupID =  array_keys($groups);

    
// Pass 'group' => group.id into search API to find members of that group
    // Only one group with that title, so we can reference
    // first result in returned array from get_groups.
    // Also pass on the required return properties and sort by criteria in params
    
$params = array(
                    
'group'            => array( $groupID[0] => 1),
                    
'return.sort_name' => 1,
                    
'return.email'     => 1,
                    
'return.phone'     => 1
                    
);
 
    
$contacts =& civicrm_contact_search( $params );
    return 
$contacts;
}



?>

Well it still doesnt work!  ???

same error still... 



Parse error: syntax error, unexpected ':', expecting ']' in /home/content/z/i/m/zimpano/html/drupal/includes/common.inc(1547) : eval()'d code on line 21




---------------------

Hi-

I dont think the error is in the script-  I tried 3 other scripts from the snippets and none of them worked all generating different parse errors.

I tried your code and it gave me

Fatal error: require_once() [function.require]: Failed opening required '../civicrm.config.php' (include_path='.:/usr/local/php5/lib/php') in /home/content/z/i/m/zimpano/html/drupal/includes/common.inc(1547) : eval()'d code on line 3

thanks...
-Prerna
« Last Edit: March 23, 2009, 05:55:38 am by psampat »

Sunil

  • I post frequently
  • ***
  • Posts: 131
  • Karma: 23
  • The community around a product more important than the product itself?
    • CiviCRM
Re: Cant get any example snippets to work - Parse error: syntax errors
March 23, 2009, 06:08:50 am
Hey
Can you save code code in the post to your drupal directory as TestAPI.php
http://forum.civicrm.org/index.php/topic,7202.msg31621.html#msg31621
changes
Add  lines
require_once('./sites/default/civicrm.settings.php');
global $civicrm_root;

remove  lines
require_once '../civicrm.config.php';


run http://youhostname/drupal/TestAPI.php

it's work for me.

if not work , then you need do search related to drupal, it's not civicrm issue
sunil
The community around a product more important than the product itself?

psampat

  • Guest
Re: Cant get any example snippets to work - Parse error: syntax errors
March 23, 2009, 07:40:14 am
This is my output

backTrace

/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Core/Error.php, backtrace, 138
, handle,
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/PEAR.php, call_user_func, 912
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/DB.php, PEAR_Error, 966
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/PEAR.php, DB_Error, 574
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/DB/common.php, raiseError, 1903
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/DB/mysql.php, raiseError, 898
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/DB/mysql.php, mysqlRaiseError, 327
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/DB/common.php, simpleQuery, 1216
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/DB/DataObject.php, query, 2394
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/packages/DB/DataObject.php, _query, 1587
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Core/DAO.php, query, 144
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Core/DAO.php, query, 862
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Contact/BAO/Query.php, executeQuery, 1962
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Contact/BAO/Query.php, savedSearch, 1926
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Contact/BAO/Query.php, group, 1010
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Contact/BAO/Query.php, whereClauseSingle, 1144
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Contact/BAO/Query.php, whereClause, 387
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Contact/BAO/Query.php, initialize, 352
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/CRM/Contact/BAO/Query.php, __construct, 2761
/home/content/z/i/m/zimpano/html/drupal/sites/all/modules/civicrm/api/v2/Contact.php, apiQuery, 231
/home/content/z/i/m/zimpano/html/drupal/testAPI.php, civicrm_contact_search, 49
/home/content/z/i/m/zimpano/html/drupal/testAPI.php, getGroupMembers, 15

unrecoverable error
    Sorry. A non-recoverable error has occurred.

    DB Error: no such field

    Database Error Code: Unknown column 'is_error' in 'where clause', 1054

    Return to home page.
Code: [Select]
Error Details:

Array
(
    [callback] => Array
        (
            [0] => CRM_Core_Error
            [1] => handle
        )

    [code] => -19
    [message] => DB Error: no such field
    [mode] => 16
    [debug_info] =>
SELECT id, cache_date, saved_search_id, children
FROM   civicrm_group
WHERE  id IN ( is_error )
  AND  ( saved_search_id != 0
   OR    saved_search_id IS NOT NULL
   OR    children IS NOT NULL )
 [nativecode=1054 ** Unknown column 'is_error' in 'where clause']
    [type] => DB_Error
    [user_info] =>
SELECT id, cache_date, saved_search_id, children
FROM   civicrm_group
WHERE  id IN ( is_error )
  AND  ( saved_search_id != 0
   OR    saved_search_id IS NOT NULL
   OR    children IS NOT NULL )
 [nativecode=1054 ** Unknown column 'is_error' in 'where clause']
    [to_string] => [db_error: message="DB Error: no such field" code=-19 mode=callback callback=CRM_Core_Error::handle prefix="" info="
SELECT id, cache_date, saved_search_id, children
FROM   civicrm_group
WHERE  id IN ( is_error )
  AND  ( saved_search_id != 0
   OR    saved_search_id IS NOT NULL
   OR    children IS NOT NULL )
 [nativecode=1054 ** Unknown column 'is_error' in 'where clause']"]
)
[/color][/code]
« Last Edit: March 23, 2009, 07:42:35 am by psampat »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Cant get any example snippets to work - Parse error: syntax errors

This forum was archived on 2017-11-26.