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
83 changes: 83 additions & 0 deletions IblockElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
$_SERVER['DOCUMENT_ROOT'] = '/var/www/workspace/kplus/';

require $_SERVER['DOCUMENT_ROOT'] .'/bitrix/modules/main/include/prolog_before.php';

use \Bitrix\Main;

class CIBlockElementCache
{
/**
* Read data from cache
*
* @param int $ttl
* @param string $cacheId
* @param string $cacheDir
*/
public static function readFromCache($ttl, $cacheId)
{
if ($ttl > 0) {
$cache = Main\Application::getInstance()->getManagedCache();

if($cache->read($ttl, $cacheId)) {
return $cache->get($cacheId);
}
}

return null;
}

/**
* Write data to cache
*
* @param array $data
* @param int $ttl
* @param string $cacheId
* @param string $cacheDir
*/
public static function writeToCache($data, $ttl, $cacheId)
{
if ($ttl > 0) {
$cache = Main\Application::getInstance()->getManagedCache();
$ret = $cache->set($cacheId, $data);
}
}

public static function GetList()
{
$args = func_get_args();
$cache = end($args);

if (isset($cache['ttl'])) {
array_pop($args);
} else {
$cache = ['ttl' => 600, 'fetchMethod' => 'Fetch'];
}

$cacheId = md5(__METHOD__ . serialize($args));
$data = static::readFromCache($cache['ttl'], $cacheId);

if ($data !== null) { echo 'dsf'; die;
return $data;
}

$result = call_user_func_array(['\CIBlockElement', __METHOD__], $args);
$return = [];

while($item = call_user_func([$result, $cache['fetchMethod'] ?: 'Fetch'])) {
$return[] = $item;
}

static::writeToCache($return, $cache['ttl']);

return $return;
}
}


$items = CIBlockElementCache::GetList(
$order = ['ID' => 'ASC'],
$filter = ['IBLOCK_ID' => 1]
);

print_r($items);
140 changes: 140 additions & 0 deletions getLastNews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

/**
* Reader of rss chanel
*/
class RssReader implements \Iterator
{
/**
* @var string
*/
protected $url;

/**
* @var XMLReader
*/
protected $source;

/**
* @var array
*/
protected $items = [];

/**
* @var integer
*/
protected $position = 0;

/**
* Construct of class
*
* @param string $url Path to rss chanel
*/
public function __construct($url)
{
$this->url = $url;
}

public function rewind()
{
$this->position = 0;
}

public function key()
{
return $this->position;
}

public function current()
{
return $this->items[$this->position];
}

public function next()
{
$this->position++;
}

public function valid()
{
if (array_key_exists($this->position, $this->items)) {
return true;
}

if ($item = $this->read()) {
$this->items[$this->position] = $item;

return true;
}

return false;
}

/**
* Open rss chanel for read
*
* @throws Exception
*
* @return DomNodeList
*/
protected function getSource()
{
if ($this->source === null)
{
$dom = new DomDocument('1.0');

if (!$dom->load($this->url)) {
throw new Exception('Unable open rss chanel');
}

$xpath = new DomXPath($dom);
$this->source = $xpath->query('//*/item');
}

return $this->source;
}

/**
* Read rss item from chanel
*
* @return array|false
*/
protected function read()
{
$items = $this->getSource();

if ($items->length <= $this->position) {
return false;
}

$node = $items->item($this->position);

return static::nodeToArray($node);
}

/**
* Convert DomNode to array
*
* @param DomNode $node
*
* @return mixed
*/
protected static function nodeToArray($node)
{
return (array) simplexml_import_dom($node);
}
}

$rss = new RssReader('https://lenta.ru/rss');

foreach ($rss as $index => $item)
{
echo 'Название: '. $item['title'], PHP_EOL;
echo 'Ссылка: '. $item['link'], PHP_EOL;
echo $item['description'], PHP_EOL;
echo '----------------------------', PHP_EOL;

if ($index >= 4) {
break;
}
}