Submitted by Jan on
/* Just a sample script on node import */ <?php define('DRUPAL_ROOT', getcwd()); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); setlocale(LC_ALL, 'zh_TW.utf8'); function fetch_remote_image($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); if ($data) { if (file_save_data($data, '/'.$filename, TRUE)) { return $filepath; } } } $filename = "./data.csv"; $log_filename = "./log.log"; global $user; $log = file_get_contents($log_filename); if (!empty($name) && ($handle = fopen($name, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { foreach($data as $k => $v) { switch ($k) { case 0: //Add node or Update node ? if (empty($v)) { $node = (object) array( 'uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'language' => LANGUAGE_NONE, 'type' => 'nodetype' ); } else { $node = node_load($v); } break; case 1: //node title $node->title = $v; break; case 2: //node body $node->body[LANGUAGE_NONE][0]['value'] = $v; break; case 3: //add taxonomy ? $term = taxonomy_get_term($v); if (!empty($term)) { $node->field_taxonomy[0]['value'] = $term->tid; $node->taxonomy = array(); $node->taxonomy[$term->tid] = $term; } else { //create taxonomy } break; case 4: //multiple field ? $data = explode(',', $v); if (!empty($data)) { foreach ($data as $data_key => $data_val) { $node->field_multiplefieldname[LANGUAGE_NONE][$data_key]['value'] = $data_val; } } break; case 5: //image field ? $path = fetch_remote_image($v); if (!empty($path)) { $file = field_file_save_file($path); $node->field_images[] = $file; } break; } } node_save($node); if ($node->nid) { $log .= $node->nid . ' : ' . $node->title .'\n'; } } file_put_contents($file, $log); exit; ?>
- Log in to post comments