Hi I spent a long time testing my code and checking it worked on our test CIVI. On the test version of the CIVI CRM the requests work no problems. As soon as I switch to live and connect to the real CIVI I'm troubled with this error. Since this works perfectly on the test CIVI CRM I'm assuming there must be some setting different within CIVI? How can I debug this? When it works on my testing environment? Does anyone know what I need to change within CIVI.

I have a feeling it is related to this bit where it tries to save the users email as Main. On live CIVI it creates two users with one as email in main one in home. On testing however (the tet CIVI) it works as expected.

    $data['contact_type'] = 'Individual';
    $data['first_name'] = $account->field_first_name['und'][0]['value'];
    $data['last_name'] = $account->field_last_name['und'][0]['value'];
    $data['email'] = $account->mail;
    $data['location_type_id'] = 'Main';

    if (isset($account->field_company_name['und'][0]['value'])) {
      $data['current_employer'] = $account->field_company_name['und'][0]['value'];

    $response = civicrm_contact_create($data);

    if (!$response->is_error) {
      $contact = reset($response->values);
      $contact_id = $contact->id;

     // search for the email to get the id.
      $email_search_params = array(
        'contact_id' => $contact_id,
        'location_type_id' => 'Home',
      $email_search_response = civicrm_email_search($email_search_params);
      $email_search_params['email'] = $account->mail;
      if (!$email_search_response->is_error && count($email_search_response->values)) {
        $email = reset($email_search_response->values);
        // Add email to Main.
        $params = array(
          'id' => $email->id,
          'location_type_id' => 'Main'
        $response2 = civicrm_email_create($params);


      // Add phone using Phone create CiviCRM API
      if ($account->field_telephone_number['und'][0]['value']) {

        $params = array(
          'phone' => $account->field_telephone_number['und'][0]['value'],
          'contact_id' => $contact_id,
          'location_type_id' => 'Main', // Other until main form completed
        $response3 = civicrm_phone_create($params);

        if (variable_get('civicrm_api_connect_debug', false)) {   
          watchdog('civicrm', '<pre>' . print_r($response3, 1) . '</pre>');

      $edit['field_civi_id']['und'][0]['value'] = $contact_id;
      // Default primary contact to the web administrator
      $edit['field_primary_contact_first_name']['und'][0]['value'] = $account->field_first_name['und'][0]['value'];
      $edit['field_primary_contact_last_name']['und'][0]['value'] = $account->field_last_name['und'][0]['value'];

This is an update:

I have added the code for civi_create_contact as requested

function civicrm_contact_create($params) {
  return civicrm_api_query('Contact', 'create', $params);

And this is the code for civicrm_api_query:

function civicrm_api_query($entity, $action, $data = array()) {
      $url = civicrm_api_connect_get_base_url() . "/sites/all/modules/civicrm/extern/rest.php";

  $keys = civicrm_api_connect_auth();

  $params = array(
    'entity' => $entity,
    'action' => $action,
    'api_key' => $keys['apikey'],
    'key' => $keys['sitekey'],
    'json' => 1,

  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded'

  // Send the HTTP basic authentication headers if we are in test mode.
  if (variable_get('civicrm_api_connect_test', false)) {
      $headers['Authorization'] = 'Basic ' .
          variable_get('civicrm_api_connect_access_username') . ':' .

  $params = array_merge($params, $data);

  $query = drupal_http_build_query($params, '', '&');

  if ($action == 'get') {
    $url .= '?' . $query;
    $method = 'GET';

  if ($action == 'create') {
    $method = 'POST';

  $data = array(
    'method' => $method,
    'headers' => $headers,
    'data' => $query,

  // Trace the submitted data if we're in debug mode
  if (variable_get('civicrm_api_connect_debug', false)) {
    watchdog('civicrm', '<pre>' . print_r($data, 1) . '</pre>');

  $response = drupal_http_request($url, $data);

  // Trace the received data if we're in debug mode
  if (variable_get('civicrm_api_connect_debug', false)) {
    watchdog('civicrm', '<pre>' . print_r($response, 1) . '</pre>');

  if ($response->code == 200) {
    $data = json_decode($response->data);

    // Trace the response if it's an error and we haven't already traced
    // it through being in debug mode.
    if ($data->is_error) {
      drupal_set_message(t('CiviCRM error @error', array('@error' => $data->error_message)), 'error');

      if (variable_get('civicrm_api_connect_debug', false)) {
        watchdog('civicrm_error', $data->error_message);
        //watchdog('civicrm_error', '<pre>' . print_r($params,1) . '</pre>');
      return false;
    else {
      return $data;
  else {
    watchdog('civicrm_error', 'CiviCRM fatal error');
    return false;
  • goulding, welcome to the stack! Your post's subject should ideally be phrased as a question. Please rephrase it?
    – JohnFF
    Feb 17, 2021 at 0:18

1 Answer 1


Welcome to SE! It looks like you are trying to create an Individual and that your API call doesn't have all the necessary data.

One option is to print out the API parameters just before you make the API call and check it has what you expect. See https://docs.civicrm.org/dev/en/latest/tools/debugging/#printing-php-variables

If you edit the question and include your code, or a link to it, then we can offer more specific suggestions.

  • Thanks for your help. I will get back to with the code example and edit my question. However I need to wait for my client to refresh the test version of CIVI and make a new test version as an exact copy from live so I can recreate the bug and test. Feb 10, 2021 at 11:28
  • I have added some code. Although i wont be able to debug as i have said this only happens on live CIVI so i need to refresh our test version. I will let you know when this happens Feb 10, 2021 at 16:45
  • Could you please clarify the context in which you are running this code, and where the function civicrm_contact_create is defined? That function does not exist in modern versions of CiviCRM. Feb 11, 2021 at 17:51
  • Thanks for adding the code. Depending how you are running it, I'd add print_r($account) at the top since I suspect that does not contain what you expect - in which case the question moves further back to how $account is being set.
    – Aidan
    Feb 12, 2021 at 9:57
  • Hi @Aidan account is defined by Drupal it is an abject for the users signed up to drupal and is part of the hook. api.drupal.org/api/drupal/modules!user!user.api.php/function/… It is passed to this hook, we can add additional fields to the user object. Hope that makes sense? Feb 12, 2021 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.