Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Source code from blog post: Batch synchronization between your system and Sugar
- http://tortuga-crm.blogspot.com/2013/05/batch-synchronization-between-your.html
- */
- ini_set("auto_detect_line_endings", true);
- define('VERBOSE', true);
- function addlog($h, $s) {
- $s=date('Y-m-d H:i:s').",$s\n";
- if (VERBOSE) echo $s;
- fputs($h, $s);
- }
- //
- // 1) LOGIN
- //
- $handlew = @fopen('logs.txt', "a");
- if (!$handlew) die("cannot write to the log file\n");
- addlog($handlew,'CMT,###START NEW LOG###');
- $url = 'https://yourdomain.sugarcrm.eu/service/v2/rest.php';
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $parameters =
- array(
- "user_auth" =>
- array(
- 'user_name' => "will",
- 'password' => md5('will'),
- ),
- "application_name" => ''
- );
- $json = json_encode($parameters);
- $postArgs = 'method=login&input_type=json&response_type=json&rest_data=' . $json;
- curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
- $response = curl_exec($curl);
- $result = json_decode($response);
- if(!is_object($result)) { die("Connection error\n"); }
- if (isset($result->number)) { // Invalid Login
- addlog($handlew,'ERROR,'.$result->name);
- fclose($handlew);
- exit;
- }
- $sessionid = $result->id;
- $userid=$result->name_value_list->user_id->value;
- //
- // 2) read CSV - get the external Id
- //
- $extidname = 'externalid_c';
- $import_file = './test_data.csv';
- $separator = ',';
- $handle = @fopen($import_file, "r");
- if (!$handle) die("cannot open the import file $import_file\n");
- $extid = array();
- $headers1 = fgetcsv($handle, 1000, ",", '"');
- $headers2 = array();
- for($i=0;$i<count($headers1);$i++) $headers2[$headers1[$i]] = $i;
- while (($a = fgetcsv($handle, 1000, ",", '"')) !== false) {
- if (count($a) <= 1) continue;
- array_push($extid, "'".$a[$headers2[$extidname]]."'");
- }
- if (!feof($handle)) die("Error: unexpected fgets() fail\n");
- fclose($handle);
- //
- // 3) Mapping: select accounts based on the external Id
- //
- $extidlst = implode(",", $extid);
- if ($extidlst!="''") {
- $extidlst = implode(",", $extid);
- $parameters = array(
- 'session' => $sessionid,
- 'module_name' => 'Accounts',
- 'query' => "$extidname IN ($extidlst)",
- 'name',
- 'offset' => 0,
- 'select_fields' => array('id', $extidname),
- 'link_name_to_fields_array' => array(),
- 'max_results' => '1000',
- 'deleted' => '0'
- );
- $json = json_encode($parameters);
- $postArgs = 'method=get_entry_list&input_type=json&response_type=json&rest_data=' . $json;
- curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
- $response = curl_exec($curl);
- $result = json_decode($response);
- $externalid2sg = array();
- foreach ($result->entry_list as $i)
- $externalid2sg[$i->name_value_list->$extidname->value] = $i->id;
- }
- //
- // 4) Import
- //
- addlog($handlew,'CMT,## search ext id');
- $handle = @fopen($import_file, "r");
- if (!$handle) die('cannot open the import file');
- $headers = array();
- $records = array();
- $logs = array();
- while (($a = fgetcsv($handle, 1000, ",", '"')) !== false) {
- if (count($a) <= 1) continue;
- if (count($headers)==0) {
- $headers = $a;
- continue;
- }
- $record = array();
- $extid=$a[$headers2[$extidname]];
- for($i=0; $i<count($a); $i++) {
- if ($headers[$i] == "NA") continue; // NA for a column you want to skip
- $field = array();
- $field['name'] = $headers[$i];
- $field['value'] = $a[$i];
- array_push($record, $field);
- }
- // case update
- if (isset($externalid2sg[$extid])) {
- $field = array();
- $field['name'] = 'id';
- $field['value'] = $externalid2sg[$extid];
- array_push($record, $field);
- addlog($handlew,"UPDATE,$extid,".$externalid2sg[$extid]);
- array_push($logs, "UPDATED,$extid,");
- } else {
- addlog($handlew,"INSERT,$extid,".$a[$headers2['name']]);
- array_push($logs, "INSERTED,$extid,");
- }
- array_push($records, $record);
- }
- if (!feof($handle)) die("Error: unexpected fgets() fail\n");
- fclose($handle);
- addlog($handlew,'CMT,## import data');
- $parameters = array(
- 'session' => $sessionid,
- 'module_name' => 'Accounts',
- 'name_value_lists' => $records
- );
- $json = json_encode($parameters);
- $postArgs = 'method=set_entries&input_type=json&response_type=json&rest_data=' . $json;
- curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
- $response = curl_exec($curl);
- $result = json_decode($response);
- $j = 0;
- foreach ($result->ids as $id)
- addlog($handlew,$logs[$j++].$id);
- curl_close($curl);
- fclose($handlew);
- /* test_data.csv:
- "externalid_c","name","phone_office","phone_fax","email1","website","billing_address_street","billing_address_postalcode","billing_address_city","billing_address_country"
- "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"
- "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"
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement