Skip to content
Valery Cheban edited this page Jan 13, 2019 · 4 revisions
  1. Пример парсинга сайта с сохранением товаров в ифноблоки

// goods update

	// test limits
	$totalPages = TotisUaParserUtils::GetCountPagerFromList( str_replace('#POROG#', 0, $urlToDetailLinks) );

	$porog = [0, $totalPages['totalSkip']];
	for ($i = $porog[0]; $i <= $porog[1]; $i+=$totalPages['totalPageLinks']) {
		$links = TotisUaParserUtils::GetDetailLinksFromPage( str_replace('#POROG#', $i, $urlToDetailLinks) );

		foreach ($links as $link) {
			if (! hiIU::checkElementExistsByNameOrCode($mainDomain . $link, $catalogIblockId, 'xml_id')) {
				$arGood = TotisUaParserUtils::GetFromDetailPageGoodParts($mainDomain . $link);
				if (trim($arGood['PROP_artikle']) == '') {
					continue;
				}
				// save good to iblock
				$r = hiIU::addElementToDb([
					'IBLOCK_ID' => $catalogIblockId,
					'ACTIVE'    => 'Y',
					'NAME'      => $arGood['NAME'],
					'PREVIEW_TEXT'  => $arGood['PREVIEW_TEXT'],
					'PREVIEW_TEXT_TYPE'  => 'text',
					'DETAIL_PICTURE'   => \CFile::MakeFileArray($arGood['DETAIL_PICTURE']),
					'XML_ID'  => $arGood['XML_ID'],
					'CODE'    => \Hipot\Utils\UnsortedUtils::TranslitText($arGood['NAME'] . '  ' . $arGood['PROP_artikle'] . ' ' . randString(3)),
					'DETAIL_TEXT'       => $arGood['DETAIL_TEXT'],
					'DETAIL_TEXT_TYPE'  => 'html'
				]);
				if ($r->STATUS == 'OK') {
					\CIBlockElement::SetPropertyValuesEx($r->RESULT, $catalogIblockId, [
						'BRAND'        => hiIU::addToHelperAndReturnElementId($arGood['PROP_manufacturer'], 5),
						'COUNTRY'      => hiIU::addToHelperAndReturnElementId($arGood['PROP_country'], 6),
						'ARTNUMBER'    => $arGood['PROP_artikle']
					]);
					hiIU::setSeoValues($catalogIblockId, $r->RESULT, [
						'ELEMENT_META_DESCRIPTION'      => $arGood['SEO_DESCRIPTION'],
						'ELEMENT_META_TITLE'            => $arGood['SEO_TITLE']
					]);
				}

				echo $r->STATUS . ' ' . $r->RESULT . PHP_EOL;
			}
		}
	}
	echo 'goods done' . PHP_EOL;

Clone this wiki locally