public static function FeedImport::processCSV in Feed Import 7.2
Imports and process a CSV file First line must contain column names!
Parameters
array $feed: Feed info array
Return value
array An array of objects
File
- ./
feed_import.inc.php, line 1268 - Feed import class for parsing and processing content.
Class
- FeedImport
- @file Feed import class for parsing and processing content.
Code
public static function processCSV(array $feed) {
// Get $length, $delimiter, $enclosure, $escape and $use_column_names settings.
extract($feed['xpath']['#settings']);
// Open CSV file.
try {
$fp = fopen($feed['url'], 'rb');
} catch (Exception $e) {
return NULL;
}
// Here will be all items.
$entities = array();
// Create a single xml object to hold each row by updating row values.
$xml = new self::$simpleXMLElement('<' . trim($feed['xpath']['#root'], '/') . '/>');
// Get first line form file.
$line = fgetcsv($fp, $length, $delimiter, $enclosure, $escape);
if ($line === FALSE) {
return NULL;
}
// Create child nodes.
if (!$use_column_names) {
foreach ($line as $index => &$col) {
$xml
->addChild('column', $col)
->addAttribute('index', $index + 1);
}
$entities[] = self::createEntity($feed, $xml);
}
else {
foreach ($line as $index => &$col) {
$child = $xml
->addChild('column', NULL);
$child
->addAttribute('index', $index + 1);
$child
->addAttribute('name', $col);
}
}
// Read file line by line.
while (($line = fgetcsv($fp, 0, $delimiter, $enclosure, $escape)) !== FALSE) {
$i = 0;
// Update created xml with new values.
foreach ($xml
->children() as $child) {
// Well, check if column exists before using it.
$child[0] = isset($line[$i]) ? $line[$i] : NULL;
unset($line[$i]);
$i++;
}
// Add to entities.
$entities[] = self::createEntity($feed, $xml);
$line = NULL;
}
try {
fclose($fp);
} catch (Exception $e) {
// Nothing to handle.
}
return $entities;
}