这是magento脚本的另外一种写法,个人觉得写的还行,高手莫见笑,用SplFileObject来处理大数据的csv文件效率高,代码如下:
getArg(&#39;file&#39;)) { $this->_files = array_merge($this->_files, array_map(&#39;trim&#39;, explode(&#39;,&#39;, $this->getArg(&#39;file&#39;)))); foreach ($this->_files as $key => $file) { $extension = self::get_extension($file); if ($extension != &#39;csv&#39;) { unset($this->_files[$key]); } } } if (emptyempty($this->_files)) { die(self::usageHelp()); } } // Shell script point of entry public function run() { self::getDataFromCsv(); if (emptyempty($this->_datas)) { die("not found data in csv ! \r\n"); } $emailBooks = array(); foreach ($this->_datas as $name => $datas) { echo "filename: {$name} =======================\r\n"; $importNum = 0; foreach ($datas as $key => $data) { if (emptyempty(trim($data[4]))) { continue; } $customerData[&#39;firstname&#39;] = trim($data[0]); $customerData[&#39;lastname&#39;] = trim($data[1]); $customerData[&#39;phone&#39;] = trim($data[2]); $customerData[&#39;mobile&#39;] = trim($data[3]); $customerData[&#39;email&#39;] = trim($data[4]); $customerData[&#39;company&#39;] = trim($data[5]); $customerData[&#39;billing_address&#39;] = $data[6] . $data[7] . $data[8]; $customerData[&#39;billing_postcode&#39;] = sprintf("%05d", trim($data[9])); $customerData[&#39;billing_city&#39;] = trim($data[10]); $customerData[&#39;billing_country&#39;] = trim($data[11]); $customerData[&#39;shipping_address&#39;] = $data[12] . $data[13] . $data[14]; if (emptyempty($data[15]) && is_numeric($data[16])) { $customerData[&#39;shipping_postcode&#39;] = sprintf("%05d", trim($data[16])); $customerData[&#39;shipping_city&#39;] = trim($data[17]); $customerData[&#39;shipping_country&#39;] = trim($data[18]); } else { $customerData[&#39;shipping_postcode&#39;] = sprintf("%05d", trim($data[15]));; $customerData[&#39;shipping_city&#39;] = trim($data[16]); $customerData[&#39;shipping_country&#39;] = trim($data[17]); } // $customerData[&#39;email&#39;] = &#39;zouhongzhao@126.com&#39;; $customerData[&#39;country&#39;] = &#39;Finland&#39;; $customerData[&#39;password&#39;] = self::randomkeys(10); & nbsp; echo "customer email {$customerData[&#39;email&#39;]} ...\r\n"; print_r($customerData); $customer = Mage::getModel(&#39;customer/customer&#39;); $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); $customer->loadByEmail($customerData[&#39;email&#39;]); if (!$customer->getId()) { echo "insert ... \r\n"; $customer->setEmail($customerData[&#39;email&#39;]); $customer->setFirstname($customerData[&#39;firstname&#39;]); $customer->setLastname($customerData[&#39;lastname&#39;]); $customer->setPassword($customerData[&#39;password&#39;]); } else { echo "update ... \r\n"; } try { $customer->save(); $customer->setConfirmation(null); $customer->save(); //Make a "login" of new customer Mage::getSingleton(&#39;customer/session&#39;)->loginById($customer->getId()); $importNum++; $emailBooks[$customerData[&#39;email&#39;]] = array( &#39;firstname&#39; => $customerData[&#39;firstname&#39;], &#39;lastname&#39; => $customerData[&#39;lastname&#39;], &#39;customer_mage_id&#39; => $customer->getId() , &#39;passwd&#39; => $customerData[&#39;password&#39;] ); echo "customer save ok !\r\n"; } catch(Exception $ex) { echo "customer save fail !\r\n"; continue; } if (trim($customerData[&#39;billing_address&#39;]) == trim($customerData[&#39;shipping_address&#39;]) && $customerData[&#39;billing_postcode&#39;] == $customerData[&#39;shipping_postcode&#39;] && $customerData[&#39;billing_city&#39;] == $customerData[&#39;shipping_city&#39;]) { $same_address = array( &#39;firstname&#39; => $customerData[&#39;firstname&#39;], &#39;lastname&#39; => $customerData[&#39;lastname&#39;], &#39;street&#39; => $customerData[&#39;billing_address&#39;], &#39;company&#39; => $customerData[&#39;company&#39;], &#39;city&#39; => $customerData[&#39;billing_city&#39;], &#39;region_id&#39; => &#39;&#39;, &#39;region&#39; => &#39;&#39;, &#39;postcode&#39; => $customerData[&#39;billing_postcode&#39;], &#39;country_id&#39; => &#39;FI&#39;, &#39;telephone&#39; => $customerData[&#39;phone&#39;], ); $customAddress = Mage::getModel(&#39;customer/address&#39;); //$customAddress = new Mage_Customer_Model_Address(); $customAddress->setData($same_address)->setCustomerId($customer->getId())->setIsDefaultBilling(&#39;1&#39;)->setIsDefaultShipping(&#39;1&#39;)->setSaveInAddressBook(&#39;1&#39;); try { $customAddress->save(); echo "sameAddress save ok !\r\n"; } catch(Exception $ex) { echo "sameAddress save fail !\r\n"; continue; } } else { $billing_address = array( &#39;firstname&#39; => $customerData[&#39;firstname&#39;], &#39;lastname&#39; => $customerData[&#39;lastname&#39;], &#39;street&#39; => $customerData[&#39;billing_address&#39;], &#39;company&#39; => $customerData[&#39;company&#39;], &#39;city&#39; => $customerData[&#39;billing_city&#39;], &#39;region_id&#39; => &#39;&#39;, &#39;region&#39; => &#39;&#39;, &#39;postcode&#39; => $customerData[&#39;billing_postcode&#39;], &#39;country_id&#39; => &#39;FI&#39;, &#39;telephone&#39; => $customerData[&#39;phone&#39;], ); self::setBillingAddress($billing_address, $customer); $shipping_address = array( &#39;firstname&#39; => $customerData[&#39;firstname&#39;], &#39;lastname&#39; => $customerData[&#39;lastname&#39;], &#39;street&#39; => $customerData[&#39;shipping_address&#39;], &#39;company&#39; => $customerData[&#39;company&#39;], &#39;city&#39; => $customerData[&#39;shipping_city&#39;], &#39;region_id&#39; => &#39;&#39;, &#39;region&#39; => &#39;&#39;, &#39;postcode&#39; => $customerData[&#39;shipping_postcode&#39;], &#39;country_id&#39; => &#39;FI&#39;, &#39;telephone&#39; => $customerData[&#39;phone&#39;], ); self::setShippingAddress($shipping_address, $customer); } // die; } echo "import num: {$importNum} =======================\r\n"; } //save passwd $fp = fopen(&#39;customer_record.log&#39;, &#39;w&#39;); fwrite($fp, json_encode($emailBooks)); fclose($fp); } public function setBillingAddress($data, $customer) { $customerAddress = Mage::getModel(&#39;customer/address&#39;); if ($defaultShippingId = $customer->getDefaultBilling()) { $customerAddress->load($defaultShippingId); } else { $customerAddress->setCustomerId($customer->getId())->setIsDefaultBilling(&#39;1&#39;)->setSaveInAddressBook(&#39;1&#39;); $customer->addAddress($customerAddress); } try { $customerAddress->addData($data)->save(); echo "BillingAddress save ok !\r\n"; } catch(Exception $e) { // Mage::log(&#39;Address Save Error::&#39; . $e->getMessage()); echo "BillingAddress save fail !\r\n"; } } public function setShippingAddress($data, $customer) { $customerAddress = Mage::getModel(&#39;customer/address&#39;); if ($defaultShippingId = $customer->getDefaultShipping()) { $customerAddress->load($defaultShippingId); } else { $customerAddress->setCustomerId($customer->getId())->setIsDefaultShipping(&#39;1&#39;)->setSaveInAddressBook(&#39;1&#39;); $customer->addAddress($customerAddress); } try { $customerAddress->addData($data)->save(); echo "ShippingAddress save ok !\r\n"; } catch(Exception $e) { // Mage::log(&#39;Address Save Error::&#39; . $e->getMessage()); echo "ShippingAddress save fail !\r\n"; } } public function randomkeys($length) { $returnStr = &#39;&#39;; $pattern = &#39;1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ&#39;; for ($i = 0; $i <$length; $i++) { $returnStr.= $pattern{mt_rand(0, 61) }; } return $returnStr; } public function getDataFromCsv() { $this->_files = array_unique($this->_files); $csvCustomers = array(); foreach ($this->_files as $filename) { echo "current file: {$filename}\r\n"; setlocale(LC_ALL, &#39;en_US.UTF-8&#39;); $cOntent= file_get_contents($filename); $data = mb_detect_encoding() ($content, &#39;UTF-8&#39;, true); // $data = iconv("CP1257","UTF-8", $content); file_put_contents($filename, $content); // fclose($handle); // print_r($content);die; $basename = basename($filename, ".csv"); $data = array(); $tmp = array(); $spl_object = new SplFileObject($filename, &#39;rb&#39;); $spl_object->seek(filesize($filename)); $start = 0; $num = $spl_object->key(); $spl_object->seek($start); while ($num-- && !$spl_object->eof()) { $data[] = $spl_object->fgetcsv(); $spl_object->next(); } foreach ($data as $key => $values) { if ($key == 0) { continue; } $mergeValue = explode(&#39;;&#39;, implode(&#39;;&#39;, $values)); // if(count($mergeValue) != 20){ // continue; // } array_push($tmp, $mergeValue); } $this->_datas[$basename] = $tmp; } return $this; } public function get_extension($filename) { return pathinfo($filename, PATHINFO_EXTENSION); } // Usage instructions public function usageHelp() { return <<run();
补充:
有一些朋友使用的是excel文档, 这样这个程序就不可以使用了, 我们可以使用phpexcel插件来读取excel文件并写入到数据库中去 .
本文地址:
转载随意,但请附上文章地址:-)