· 6 years ago · Feb 16, 2020, 04:26 PM
1<?php
2
3 $scriptActive = 0;
4 $script_function = 1; // 0 for add ,1 for update
5 $postaction = "addclient";
6 if( $script_function==1 ){
7 $postaction = "updateclient";//updateclient,addclient
8 } else {
9 $postaction = "addclient";
10 }
11 $starting_uuid = 4;
12 $url = "/includes/api.php"; # URL to WHMCS API file
13 $username = "username"; # Admin username goes here
14 $password = "pa$$word"; # Admin password goes here
15
16 $firstname = "Dispensary";
17 $lastname = "Owner";
18 $country = "US";
19 $clientgroup = "Imported Dispensary";
20 $marketing_emails = "1";
21 $send_new_acct_message = "0";
22
23 // Set path to CSV file
24 $fileDirectory = dirname(__FILE__)."/csvfile/";
25 $csvFile = "customer.csv";
26 $errCsvFile = "errors.csv";
27 $csvFile = ($fileDirectory.$csvFile);
28 $errFile = fopen($fileDirectory.$errCsvFile,"a");
29 $csvRowArray = readCSV($csvFile);
30 echo "<pre>";
31 /*print_r($csvRowArray);
32 exit;*/
33
34 /******************************************************************************/
35
36 function readCSV($csvFile){
37
38 if (file_exists($csvFile)) {
39
40 $file_handle = fopen($csvFile, 'r');
41 while (!feof($file_handle) ) {
42 $line_of_text[] = fgetcsv($file_handle, 1024);
43
44 }
45 fclose($file_handle);
46
47 //REMOVE FIRST LINE (WITH HEADERS)
48 unset($line_of_text[0]);
49
50 // RESET INDEXES & REMOVE EMPTY ARRAY ELEMENTS
51 $line_of_text = array_values($line_of_text);
52 $line_of_text = array_filter( $line_of_text );
53
54 return $line_of_text;
55
56 } else {
57 echo "<h1>$csvFile does not exist</h1>";
58 die;
59 }
60
61 }
62
63 ob_start(); //Turning ON Output Buffering
64
65 $postfields["username"] = $username;//DO NOT CHANGE
66 $postfields["password"] = md5($password);//DO NOT CHANGE
67 $postfields["action"] = $postaction; # action performed by the [[API:Functions]]
68
69 $all_processed_emails = array();
70 $error_heading = array( 'Lic#','Error Reason','Time');
71 fputcsv($errFile,$error_heading);
72 $counter = $starting_uuid;
73 foreach($csvRowArray as $key => $csvRow) {
74 if($script_function==0){
75 $curr_email = strtolower($csvRow[3]);
76 if( in_array($curr_email,$all_processed_emails) ){
77 #echo "<br>Error : ".$curr_email;
78 $errors = array(ucfirst($csvRow[0]), 'Email Duplicate : '.$curr_email,date('y-m-d H:i:s'));
79 fputcsv($errFile,$errors);
80 continue; //Skip if email find already
81 }
82 $all_processed_emails[] = strtolower($csvRow[3]);
83
84 //$postfields["userid"] = $counter;
85 $counter++;
86 $postfields["firstname"] = ucfirst($firstname);
87 $postfields["lastname"] = ucfirst($lastname);
88 $postfields["companyname"] = ucfirst($csvRow[1]);
89 $postfields["email"] = strtolower($csvRow[3]);
90 //$postfields["address1"] = ucfirst($csvRow[4]);
91 $postfields["city"] = ucfirst($csvRow[4]);
92 $postfields["state"] = strtoupper($csvRow[7]);
93 $postfields["postcode"] = $csvRow[5];
94 $postfields["country"] = strtoupper($country);
95 $postfields["phonenumber"] = preg_replace("/[^0-9]/","",$csvRow[2]);
96 $postfields["License_ID"] = ucfirst($csvRow[0]);
97 $postfields["customfields"] = base64_encode(serialize(array(
98 "1" => ucfirst($csvRow[0]),
99 "6" => $clientgroup
100 )));
101 $postfields["noemail"] = $marketing_emails;
102 $postfields["marketing_emails"] = $marketing_emails;
103 $postfields["send_new_acct_message"] = $send_new_acct_message;
104 //print_r($postfields);
105 } else {
106 if( !isset($csvRow[0]) || trim(ucfirst($csvRow[0]))=='' ){
107 $errors = array('N/A', 'No Lic Present : '.$csvRow[0],date('y-m-d H:i:s'));
108 fputcsv($errFile,$errors);
109 continue; //Skip if email find already
110 }
111 $postfields["License_ID"] = ucfirst($csvRow[0]);
112 $postfields["companyname"] = ucfirst($csvRow[1]);
113 $postfields["address1"] = ucfirst($csvRow[2]);
114 $postfields["city"] = ucfirst($csvRow[3]);
115 $postfields["postcode"] = $csvRow[4];
116 $postfields["state"] = strtoupper($csvRow[6]);
117 $postfields["customfields"] = base64_encode(serialize(array(
118 "1" => ucfirst($csvRow[0]),
119 "6" => $clientgroup
120 )));
121 $postfields["noemail"] = $marketing_emails;
122 $postfields["marketing_emails"] = $marketing_emails;
123 $postfields["send_new_acct_message"] = $send_new_acct_message;
124 //print_r($postfields);
125 }
126
127 if ($scriptActive==1) {
128 $ch = curl_init();
129 curl_setopt($ch, CURLOPT_URL, $url);
130 curl_setopt($ch, CURLOPT_POST, 1);
131 curl_setopt($ch, CURLOPT_TIMEOUT, 100);
132 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
133 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
134 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
135 $data = curl_exec($ch);
136 $data = explode(";",$data);
137 foreach ($data AS $temp) {
138 $temp = explode("=",$temp);
139 $results[$temp[0]] = $temp[1];
140 }
141 ob_flush();//Flush the data here
142 if ($results["result"]=="success") {
143 # Result was OK!
144 echo $postfields["firstname"]." ".$postfields["lastname"]." was successfully added!";
145 } else {
146 # An error occured
147 echo "The following error occured: ".$results["message"];
148 $errors = array(ucfirst($csvRow[0]), $results["message"],date('y-m-d H:i:s'));
149 fputcsv($errFile,$errors);
150 }
151 curl_close($ch);
152 }
153 }
154
155 if ($scriptActive!=1) {
156 echo "Import File: " . $csvFile . "<br><br>";
157 echo '<pre>';
158 print_r($csvRowArray);
159 echo '</pre>';
160 }
161
162 ob_end_flush();