在向SuiteCRM添加潜在客户时如何防止重复?

时间:2017-11-17 21:42:12

标签: php json rest crm suitecrm

developer guide for SuiteCRM之前的old SugarCRM one相比,software fork有点不完整(至少在2017年第四季度)。因此,通过下载WordPress Plugin for SugarCRM,我能够找到REST API和JSON,以便使用以下代码将销售线索添加到SuiteCRM中。

现在如何防止家庭电话,手机或电子邮件地址重复?

<?php

error_reporting(E_ALL);
ini_set('display_errors','On');

header('Content-Type: text/plain');

CRM::loginCRM('admin','xxxxPASSWORDxxxxxx');

$aResult = CRM::addLead(array(
  'name' => 'John Doe',
  'description' => 'sample description',
  'salutation' => 'Mr.',
  'first_name' => 'John',
  'last_name' => 'Doe',
  'do_not_call' => 'No',
  'phone_home' => '202-111-2222',
  'phone_mobile' => '202-111-2222',
  'email1' => 'test@example.com',
  'primary_address_street' => '123 Main Street',
  'primary_address_street2' => '',
  'primary_address_street3' => '',
  'primary_address_city' => 'New York',
  'primary_address_state' => 'NY'
));

print_r($aResult);
CRM::logoutCRM();


die('OK');
/////////////////////////

class CRM {

private static $SessionID = '';

private static $URL = 'https://mycrmserver-example.com/service/v4_1/rest.php';

private static $User = '';
private static $Shadow = '';

public static function sendJSON($a) {
  $s = file_get_contents(
    self::$URL,
    false,
    stream_context_create(
      array(
        'http' => array (
          'method' => 'POST',
          'header' => 'Content-Type: application/x-www-form-urlencoded',
          'content' => http_build_query($a)
        )
      )
    )
  );
  $a2 = json_decode($s);
  return $a2;
}

public static function loginCRM($sUser,$sPass) {
  $sShadow = md5($sPass);
  self::$User = $sUser;
  self::$Shadow = $sShadow;
  $asLogin = array (
    'method' => 'login',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'user_auth' => array(
        'user_name' => $sUser,
        'password' => $sShadow,
        'version' => 1
      ),
      'application_name' => 'RestTest',
      'name_value_list' => array()
    ))
  );
  $a = self::sendJSON($asLogin);
  self::$SessionID = $a->id;
}

public static function logoutCRM() {
  $asLogin = array (
    'method' => 'logout',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'user_auth' => array(
        'user_name' => self::$User,
        'password' => self::$Shadow,
        'version' => 1
      ),
      'application_name' => 'RestTest',
      'name_value_list' => array()
    ))
  );
  self::sendJSON($asLogin);
}

public static function addLead($a) {
  $asNameValueList = array();
  foreach($a as $sKey => $sVal) {
    $asNameValueList[] = array('name'=>$sKey,'value'=>$sVal);
  }
  $asAddEntry = array (
    'method' => 'set_entry',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'session' => self::$SessionID,
      'module_name' => 'Leads',
      'name_value_list' => $asNameValueList
    ))
  );
  $a = self::sendJSON($asAddEntry);
  return $a;
}

} // end CRM

1 个答案:

答案 0 :(得分:2)

将这些功能添加到CRM类中,并在添加潜在客户之前进行检查。我从this answer获得了一些帮助,偶然给了我一些见解。此外,我建议您做一些事情来加强安全性,例如添加.htaccess或NGINX规则,只允许某些IP地址或要求某些标头到达/ service文件夹中的任何内容,以及/ service / *子文件夹通过HTTP或HTTPS。

public static function leadExistsByPhone($sHomePhone,$sMobilePhone) {
  $sHomePhone = (empty($sHomePhone)) ? 'xxxxxinvalid' : $sHomePhone;
  $sMobilePhone = (empty($sMobilePhone)) ? 'xxxxxinvalid' : $sMobilePhone;
  $asCheck = array (
    'method' => 'get_entry_list',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'session' => self::$SessionID,
      'module_name' => 'Leads',
      'query' => "
        leads.phone_home = '$sHomePhone'
        OR leads.phone_mobile = '$sMobilePhone'
      ",
      'order_by' => 'leads.date_entered DESC',
      'offset' => '0',
      'select_fields' => array(),
      'link_name_to_fields_array' => array(),
      'max_results' => 999999,
      'deleted' => false
    ))
  );
  $a = self::sendJSON($asCheck);
  $nCount = @ $a->result_count;
  $nCount = intval($nCount);
  return ($nCount > 0);
}

public static function leadExistsByEmail($sEmail) {
  if (!filter_var($sEmail, FILTER_VALIDATE_EMAIL)) {
    die('DENIED: invalid email address format');
  }
  $asCheck = array (
    'method' => 'get_entry_list',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'session' => self::$SessionID,
      'module_name' => 'Leads',
      'query' => "
        leads.id IN
        (
          SELECT email_addr_bean_rel.bean_id
          FROM   email_addr_bean_rel
          JOIN   email_addresses
          ON     email_addr_bean_rel.email_address_id = email_addresses.id
          WHERE
          email_addresses.email_address = '$sEmail'
        )
      ",
      'order_by' => 'leads.date_entered DESC',
      'offset' => '0',
      'select_fields' => array(),
      'link_name_to_fields_array' => array(),
      'max_results' => 999999,
      'deleted' => false
    ))
  );
  $a = self::sendJSON($asCheck);
  $nCount = @ $a->result_count;
  $nCount = intval($nCount);
  return ($nCount > 0);
}