Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions IBlockHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/bitrix/modules/main/include/prolog_before.php");

if(!CModule::IncludeModule('iblock'))
die('iblock module not found, operation impossible');

/**
* Class IBlockHelper
*
*/
class IBlockHelper extends CIBlockElement{

protected $PCache;

function __construct()
{
$this->PCache = new CPHPCache();
}

/**
* @param $arr
*/
public function pr($arr){
echo '<pre>';
print_r($arr);
echo '</pre>';
}

/**
* @param $arr
* @return array
*/
private function getParams($arr){

// значения по умолчанию
$defaultParams = Array(
// сколько записей выбрать
'count' => 10,

// кеширование
'cache' => Array(

// нужно ли кешировать вывод true / false
'run'=>true,

// ручное создание идентификатора кеша
// если передать false, идетификатор будет
// построен от объеденения массива filter
'id'=>false,

// сколько держать кеш
'time'=>30,

// удалить ли принудительно кеш
'destroy' => false
),

// как сортировать
'order' => Array('SORT'=>'DESC'),

// по каким полям выбирать
'filter' => Array("IBLOCK_ID"=>3, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y"),

// какие поля выбирать
'select' => Array("NAME", "ID")
);

return array_merge($defaultParams, $arr);

}

/**
* @param $arParams
* @return array
*/
protected function _GetList($arParams){
$arResult = Array();

$res = self::GetList($arParams['order'], $arParams['filter'], false, Array("nPageSize"=>$arParams['count']), $arParams['select']);

while($ob = $res->GetNextElement()) {
$arResult[] = $ob->GetFields();
}

return $arResult;
}

/**
* @param $arParams
* @return array
*/
public function GetElements($arParams=Array()){

$arResult = Array();
$arParams = $this->getParams($arParams);

// формируем идентификатор кеша в зависимости от всех параметров
// которые могут повлиять на результирующий HTML
// !! или пишем кеш разработчика

if($arParams['cache']['id']){
$cache_id = $arParams['cache']['id'];
}else{
$cache_id = 'helper_'.implode("_", $arParams['filter']);
}

// если не надо использовать кеш
// сразу вернем элементы инфоблока
if(!$arParams['cache']['run'])
return $this->_GetList($arParams);

//если надо принудительно очистить кеш
if($arParams['cache']['destroy']) {
$this->PCache->CleanDir();
}

// если кеш есть и он ещё не истек, то
if($this->PCache->InitCache($arParams['cache']['time'], $cache_id, "/")){

// получаем закешированные переменные
$vars = $this->PCache->GetVars();
return $vars['arrElements'];
}

// начинаем буферизирование вывода
if($this->PCache->StartDataCache()){

$arResult = $this->_GetList($arParams);

// записываем предварительно буферизированный вывод в файл кеша
// вместе с дополнительной переменной
$this->PCache->EndDataCache(array(
"arrElements" => $arResult
));

}

return $arResult;

}
}

$IBlockHelper = new IBlockHelper();

$elements = $IBlockHelper->GetElements(Array(
'count' => 3,
'cache' => Array(
'time'=>30,
'id'=>false,
'run'=>true,
'destroy' =>false,
),
'select' => Array("NAME", "ID"),
'order' => Array('SORT'=>'DESC'),
'filter' => Array("IBLOCK_ID"=>1, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y"),
));

$IBlockHelper->pr($elements);
?>
46 changes: 46 additions & 0 deletions getLastNews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

class RSSParse {

protected $url;

function __construct($url)
{
$this->url = $url;
}

public function run(){

$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);

$response = file_get_contents($this->url, false, stream_context_create($arrContextOptions));

$rss = simplexml_load_string($response);

$index = 0;

echo "\n\r ===================================================\n\r";

foreach ($rss->channel->item as $item) {

if($index>4) break;

echo "\n\r";
echo "Link: ".$item->link."\n\r";
echo "Title: ".$item->title."\n\r";
echo "Description: ".$item->description."\n\r";
echo "===================================================\n\r";


$index ++;
}
}
}

$rssParser = new RSSParse('https://lenta.ru/rss');
$rssParser->run();
Binary file added screan_parse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.