Парсим элементы
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php die('Все спарсили'); require($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/include/prolog_before.php'); CModule::IncludeModule("iblock"); // инфоблоки function getItems($sql) { $DB = new CDatabase; $DBHost = "localhost"; $DBLogin = "LOGIN к БД MODX"; $DBPassword = "Пароль к БД modx"; $DBName = "имя базы modX"; $DB->Connect($DBHost, $DBName, $DBLogin, $DBPassword); $query = $DB->Query($sql); if ($query->SelectedRowsCount() > 0) { $res = []; while($data = $query->Fetch()) { array_push($res, $data); } return $res; } else if ($query->SelectedRowsCount() == 0) { return false; } $DB->Disconnect(); } function translit($string) { $arTransParams = array( "max_len" => 100, "change_case" => false, "replace_space" => '_', "replace_other" => '-', "delete_repeat_replace" => true, ); $transName = CUtil::translit($string, "ru", $arTransParams); return $transName; } $items = getItems("SELECT * FROM `modx_site_content` where parent = 28"); $el = new CIBlockElement; foreach ($items as $item) { $id = $item['id']; $title = $item['pagetitle']; $desc = $item['description']; $previewText = $item['introtext']; $detailText = $item['content']; $prop = [2 => $id]; $fields = [ "IBLOCK_SECTION_ID" => false, "IBLOCK_ID" => 6, "NAME" => $title, "CODE" => translit($title), "ACTIVE" => "Y", "PROPERTY_VALUES" => $prop, "PREVIEW_TEXT" => $previewText, "DETAIL_TEXT" => $detailText, "IPROPERTY_TEMPLATES" => [ "ELEMENT_META_DESCRIPTION" => $desc ] ]; if ($element = $el->Add($fields)) { echo "New ID: ".$element; } else { echo "Error: ".$el->LAST_ERROR; } echo "<hr/>"; } |
Парсим картинки (Галерею)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php require($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/include/prolog_before.php'); CModule::IncludeModule("iblock"); // инфоблоки function getItems($sql) { $DB = new CDatabase; $DBHost = "localhost"; $DBLogin = "логин modx"; $DBPassword = "пароль modx"; $DBName = "база modx"; $DB->Connect($DBHost, $DBName, $DBLogin, $DBPassword); $query = $DB->Query($sql); if ($query->SelectedRowsCount() > 0) { $res = []; while($data = $query->Fetch()) { array_push($res, $data); } return $res; } else if ($query->SelectedRowsCount() == 0) { return false; } $DB->Disconnect(); } function galleryImage ($id) { $sql = 'SELECT items.id, items.item, item.filename FROM `modx_gallery_albums` as album LEFT JOIN `modx_gallery_album_items` as items ON items.album = album.id LEFT JOIN `modx_gallery_items` as item ON item.id = items.item where album.name = "album'.$id.'"'; $items = getItems($sql); $arr = []; foreach ($items as $key) { $file = ["VALUE" => CFile::MakeFileArray("/var/www/user/data/www/siteMODX.ru/assets/gallery/" . $key['filename'])]; array_push($arr, $file); } return $arr; } $arSelect = Array("ID", "NAME", "DATE_ACTIVE_FROM", "PROPERTY_ID_GALLERY"); $arFilter = Array("IBLOCK_ID" => 6, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y", "PROPERTY_GALLERY" => false); $res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize" => 500), $arSelect); $el = new CIBlockElement; while($ob = $res->GetNext()) { $elementID = $ob['ID']; $galleryID = $ob['PROPERTY_ID_GALLERY_VALUE']; $images = galleryImage($galleryID); $prop = [ 1 => $images, 2 => $galleryID ]; $fields = [ "PROPERTY_VALUES" => $prop ]; $el->Update($elementID, $fields); } |
#45 строка
Можно указать абсолютный путь или внешний url расположение картинки в текущем примере рассмотрен вариант абсолютный путь до изображения (сайт bitrix и modx) находятся на одном сервере