Разработка бота-парсера прайс-листов поставщиков (Excel/CSV/XML)
Поставщики присылают прайс-листы в разных форматах: Excel с нестандартной структурой, CSV с кириллицей в разных кодировках, XML разной степени стандартизированности. Задача парсера — нормализовать всё это в единый формат и синхронизировать с каталогом магазина.
Типичные проблемы входящих файлов
- Excel: данные начинаются с 3-й строки, заголовки — в объединённых ячейках
- CSV: кодировка windows-1251, разделитель — точка с запятой, цены с пробелами
- XML: нестандартные теги, пространства имён, атрибуты вместо значений
- Несогласованные форматы SKU у разных поставщиков
- Числа как строки, даты как числа Excel
Парсер Excel (PhpSpreadsheet)
// app/Services/PriceList/ExcelParser.php
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
class ExcelParser
{
private array $columnMap = [];
public function parse(string $filePath, array $config): array
{
$spreadsheet = IOFactory::load($filePath);
$sheet = $spreadsheet->getSheetByName($config['sheet_name'] ?? null)
?? $spreadsheet->getActiveSheet();
$headerRow = $config['header_row'] ?? 1;
$dataStartRow = $config['data_start_row'] ?? 2;
// Определяем маппинг колонок по заголовкам
$this->detectColumns($sheet, $headerRow, $config['column_aliases']);
$products = [];
$highestRow = $sheet->getHighestRow();
for ($row = $dataStartRow; $row <= $highestRow; $row++) {
$sku = $this->getCellValue($sheet, $this->columnMap['sku'], $row);
if (empty($sku)) continue;
$products[] = [
'sku' => trim($sku),
'name' => $this->getCellValue($sheet, $this->columnMap['name'] ?? null, $row),
'price' => $this->parsePrice($this->getCellValue($sheet, $this->columnMap['price'], $row)),
'in_stock' => $this->parseStock($this->getCellValue($sheet, $this->columnMap['stock'] ?? null, $row)),
'category' => $this->getCellValue($sheet, $this->columnMap['category'] ?? null, $row),
];
}
return $products;
}
private function detectColumns(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet, int $headerRow, array $aliases): void
{
$highestCol = $sheet->getHighestColumn();
$highestColIndex = Coordinate::columnIndexFromString($highestCol);
for ($col = 1; $col <= $highestColIndex; $col++) {
$header = strtolower(trim(
$sheet->getCellByColumnAndRow($col, $headerRow)->getValue() ?? ''
));
foreach ($aliases as $fieldName => $possibleHeaders) {
foreach ($possibleHeaders as $alias) {
if (str_contains($header, strtolower($alias))) {
$this->columnMap[$fieldName] = $col;
break 2;
}
}
}
}
}
private function parsePrice(mixed $value): float
{
if (is_numeric($value)) return (float) $value;
return (float) preg_replace('/[^\d.]/', '', str_replace(',', '.', (string) $value));
}
private function parseStock(mixed $value): bool
{
if (is_null($value)) return true;
$s = strtolower(trim((string) $value));
return !in_array($s, ['0', 'нет', 'no', 'false', 'отсутствует', '']);
}
}
Конфигурация под конкретного поставщика:
// config/price_list_parsers.php
return [
'supplier_abc' => [
'format' => 'excel',
'sheet_name' => 'Прайс',
'header_row' => 2,
'data_start_row' => 3,
'column_aliases' => [
'sku' => ['Артикул', 'SKU', 'Код товара'],
'name' => ['Наименование', 'Название', 'Товар'],
'price' => ['Цена', 'Цена, руб.', 'Стоимость'],
'stock' => ['Остаток', 'Наличие', 'Кол-во'],
'category' => ['Категория', 'Группа', 'Раздел'],
],
],
];
Парсер CSV с автоопределением кодировки
// app/Services/PriceList/CsvParser.php
class CsvParser
{
public function parse(string $filePath, array $config = []): array
{
$content = file_get_contents($filePath);
// Автоопределение кодировки
$encoding = mb_detect_encoding($content, ['UTF-8', 'Windows-1251', 'KOI8-R'], true);
if ($encoding && $encoding !== 'UTF-8') {
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
}
// Автоопределение разделителя
$delimiter = $config['delimiter'] ?? $this->detectDelimiter($content);
$lines = str_getcsv($content, "\n");
$headers = str_getcsv(array_shift($lines), $delimiter);
$headers = array_map('trim', $headers);
$products = [];
foreach ($lines as $line) {
if (empty(trim($line))) continue;
$row = str_getcsv($line, $delimiter);
if (count($row) !== count($headers)) continue;
$data = array_combine($headers, $row);
$products[] = $this->normalizeRow($data, $config);
}
return $products;
}
private function detectDelimiter(string $content): string
{
$firstLine = strtok($content, "\n");
$counts = [
',' => substr_count($firstLine, ','),
';' => substr_count($firstLine, ';'),
"\t" => substr_count($firstLine, "\t"),
];
return array_key_first(array_filter($counts, fn($c) => $c === max($counts)));
}
private function normalizeRow(array $row, array $config): array
{
$map = $config['field_map'] ?? [];
return [
'sku' => trim($row[$map['sku'] ?? 'sku'] ?? ''),
'name' => trim($row[$map['name'] ?? 'name'] ?? ''),
'price' => $this->parsePrice($row[$map['price'] ?? 'price'] ?? 0),
'in_stock' => !empty($row[$map['stock'] ?? 'stock']),
];
}
}
Парсер XML
// app/Services/PriceList/XmlParser.php
class XmlParser
{
public function parse(string $filePath, array $config): array
{
$xml = simplexml_load_file($filePath, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($xml === false) {
throw new \RuntimeException("Не удалось разобрать XML: " . $filePath);
}
// XPath для извлечения товаров
$itemXpath = $config['item_xpath'] ?? '//item';
$items = $xml->xpath($itemXpath);
return array_map(fn($item) => $this->extractItem($item, $config), $items);
}
private function extractItem(\SimpleXMLElement $item, array $config): array
{
$fields = $config['fields'] ?? [];
$extract = function(string $path) use ($item): string {
// Поддержка атрибутов (@attr) и дочерних элементов (tag)
if (str_starts_with($path, '@')) {
return (string) ($item->attributes()[substr($path, 1)] ?? '');
}
$nodes = $item->xpath($path);
return $nodes ? trim((string) $nodes[0]) : '';
};
return [
'sku' => $extract($fields['sku'] ?? 'article'),
'name' => $extract($fields['name'] ?? 'name'),
'price' => (float) $extract($fields['price'] ?? 'price'),
'in_stock' => $extract($fields['stock'] ?? 'available') !== '0',
'category' => $extract($fields['category'] ?? 'category'),
];
}
}
Фасад-диспетчер форматов
// app/Services/PriceList/PriceListParser.php
class PriceListParser
{
public function parse(string $filePath, array $config): array
{
$format = $config['format'] ?? $this->detectFormat($filePath);
return match ($format) {
'excel', 'xlsx', 'xls' => app(ExcelParser::class)->parse($filePath, $config),
'csv' => app(CsvParser::class)->parse($filePath, $config),
'xml' => app(XmlParser::class)->parse($filePath, $config),
default => throw new \InvalidArgumentException("Неизвестный формат: {$format}"),
};
}
private function detectFormat(string $filePath): string
{
return match (strtolower(pathinfo($filePath, PATHINFO_EXTENSION))) {
'xlsx', 'xls', 'ods' => 'excel',
'csv', 'txt' => 'csv',
'xml' => 'xml',
default => 'csv',
};
}
}
Автоматическое получение файлов
Прайс-листы поступают по email, FTP или HTTP. Для каждого канала — отдельный получатель:
// app/Jobs/FetchSupplierPriceList.php
class FetchSupplierPriceList implements ShouldQueue
{
public function handle(PriceListFetcher $fetcher, PriceListParser $parser): void
{
$supplier = Supplier::findOrFail($this->supplierId);
$filePath = match ($supplier->price_list_source) {
'ftp' => $fetcher->downloadFromFtp($supplier),
'url' => $fetcher->downloadFromUrl($supplier->price_list_url),
'email' => $fetcher->fetchFromEmail($supplier),
default => throw new \RuntimeException('Неизвестный источник прайс-листа'),
};
$config = config("price_list_parsers.{$supplier->config_key}");
$products = $parser->parse($filePath, $config);
foreach (array_chunk($products, 500) as $chunk) {
ImportPriceListChunk::dispatch($supplier->id, $chunk);
}
}
}
Срок разработки
Парсер для 1 поставщика (1 формат, стандартная структура): 2-4 рабочих дня. Универсальный диспетчер + 5 поставщиков с разными форматами: 7-10 рабочих дней.







