SHARE
TWEET

Batch synchronization between your system and SugarCRM

onepomiachty May 24th, 2013 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Source code from blog post: Batch synchronization between your system and Sugar
  4. http://tortuga-crm.blogspot.com/2013/05/batch-synchronization-between-your.html
  5. */
  6. ini_set("auto_detect_line_endings", true);
  7. define('VERBOSE', true);
  8.  
  9. function addlog($h, $s) {
  10.         $s=date('Y-m-d H:i:s').",$s\n";
  11.         if (VERBOSE) echo $s;
  12.         fputs($h, $s);
  13. }
  14.  
  15. //
  16. // 1) LOGIN
  17. //
  18. $handlew = @fopen('logs.txt', "a");
  19. if (!$handlew) die("cannot write to the log file\n");
  20. addlog($handlew,'CMT,###START NEW LOG###');
  21. $url = 'https://yourdomain.sugarcrm.eu/service/v2/rest.php';
  22. $curl = curl_init($url);
  23. curl_setopt($curl, CURLOPT_POST, true);
  24. curl_setopt($curl, CURLOPT_HEADER, false);
  25. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  26. $parameters =
  27.   array(
  28.         "user_auth" =>
  29.                 array(
  30.                 'user_name' => "will",
  31.                 'password' => md5('will'),
  32.                 ),
  33.         "application_name" => ''
  34.   );
  35. $json = json_encode($parameters);
  36. $postArgs = 'method=login&input_type=json&response_type=json&rest_data=' . $json;
  37. curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
  38. $response = curl_exec($curl);
  39. $result = json_decode($response);
  40. if(!is_object($result)) { die("Connection error\n"); }
  41. if (isset($result->number)) { // Invalid Login
  42.         addlog($handlew,'ERROR,'.$result->name);
  43.         fclose($handlew);
  44.         exit;
  45. }
  46. $sessionid = $result->id;
  47. $userid=$result->name_value_list->user_id->value;
  48.  
  49. //
  50. // 2) read CSV - get the external Id
  51. //
  52. $extidname = 'externalid_c';
  53. $import_file = './test_data.csv';
  54. $separator = ',';
  55. $handle = @fopen($import_file, "r");
  56. if (!$handle) die("cannot open the import file $import_file\n");
  57. $extid = array();
  58. $headers1 = fgetcsv($handle, 1000, ",", '"');
  59. $headers2 = array();
  60. for($i=0;$i<count($headers1);$i++) $headers2[$headers1[$i]] = $i;
  61. while (($a = fgetcsv($handle, 1000, ",", '"')) !== false) {
  62.         if (count($a) <= 1) continue;
  63.     array_push($extid, "'".$a[$headers2[$extidname]]."'");
  64. }
  65. if (!feof($handle)) die("Error: unexpected fgets() fail\n");
  66. fclose($handle);
  67.  
  68. //
  69. // 3) Mapping: select accounts based on the external Id
  70. //
  71. $extidlst = implode(",", $extid);
  72. if ($extidlst!="''") {
  73.         $extidlst = implode(",", $extid);
  74.         $parameters = array(
  75.                 'session' => $sessionid,
  76.                 'module_name' => 'Accounts',
  77.                 'query' => "$extidname IN ($extidlst)",
  78.                 'name',
  79.                 'offset' => 0,
  80.                 'select_fields' => array('id', $extidname),
  81.                 'link_name_to_fields_array' => array(),
  82.                 'max_results' => '1000',
  83.                 'deleted' => '0'
  84.         );
  85.         $json = json_encode($parameters);
  86.         $postArgs = 'method=get_entry_list&input_type=json&response_type=json&rest_data=' . $json;
  87.         curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
  88.         $response = curl_exec($curl);
  89.         $result = json_decode($response);
  90.         $externalid2sg = array();
  91.         foreach ($result->entry_list as $i)
  92.                 $externalid2sg[$i->name_value_list->$extidname->value] = $i->id;
  93. }
  94.  
  95. //
  96. // 4) Import
  97. //
  98. addlog($handlew,'CMT,## search ext id');
  99. $handle = @fopen($import_file, "r");
  100. if (!$handle) die('cannot open the import file');
  101. $headers = array();
  102. $records = array();
  103. $logs = array();
  104.  
  105. while (($a = fgetcsv($handle, 1000, ",", '"')) !== false) {
  106.         if (count($a) <= 1) continue;
  107.     if (count($headers)==0) {
  108.         $headers = $a;
  109.         continue;
  110.     }
  111.     $record = array();
  112.     $extid=$a[$headers2[$extidname]];
  113.     for($i=0; $i<count($a); $i++) {
  114.         if ($headers[$i] == "NA") continue; // NA for a column you want to skip
  115.         $field = array();
  116.                 $field['name'] = $headers[$i];
  117.                 $field['value'] = $a[$i];
  118.         array_push($record, $field);
  119.     }
  120.         // case update
  121.         if (isset($externalid2sg[$extid])) {
  122.         $field = array();
  123.                 $field['name'] = 'id';
  124.                 $field['value'] = $externalid2sg[$extid];
  125.         array_push($record, $field);
  126.         addlog($handlew,"UPDATE,$extid,".$externalid2sg[$extid]);
  127.         array_push($logs, "UPDATED,$extid,");
  128.         } else {
  129.                 addlog($handlew,"INSERT,$extid,".$a[$headers2['name']]);
  130.                 array_push($logs, "INSERTED,$extid,");
  131.         }
  132.         array_push($records, $record);
  133. }
  134. if (!feof($handle)) die("Error: unexpected fgets() fail\n");
  135. fclose($handle);
  136.  
  137. addlog($handlew,'CMT,## import data');
  138. $parameters = array(
  139.     'session' => $sessionid,
  140.     'module_name' => 'Accounts',
  141.     'name_value_lists' => $records  
  142. );
  143. $json = json_encode($parameters);
  144. $postArgs = 'method=set_entries&input_type=json&response_type=json&rest_data=' . $json;
  145. curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
  146. $response = curl_exec($curl);
  147. $result = json_decode($response);
  148. $j = 0;
  149. foreach ($result->ids as $id)
  150.         addlog($handlew,$logs[$j++].$id);
  151. curl_close($curl);
  152. fclose($handlew);
  153.  
  154. /* test_data.csv:
  155. "externalid_c","name","phone_office","phone_fax","email1","website","billing_address_street","billing_address_postalcode","billing_address_city","billing_address_country"
  156. "abc124","Acme 14","05 55 11 22 32","05 55 11 22 34","contact@acme14.com","www.acme14.com","41 RUE WINSTON CHURCHILL",87000,"LIMOGES","FRANCE"
  157. "abc125","Acme 15","01 40 55 66 77","01 40 55 66 78","contact@acme15.org","www.acme15.org","6 PLACE DE LA MADELEINE",75008,"PARIS","FRANCE"
  158. */
  159. ?>
RAW Paste Data
Top