PHP code example of vamischenko / xmlfeeds

1. Go to this page and download the library: Download vamischenko/xmlfeeds library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vamischenko / xmlfeeds example snippets


use XmlFeeds\Builder\FeedBuilder;
use XmlFeeds\Platforms\YandexNews;

$xml = FeedBuilder::for(new YandexNews())
    ->title('Мой сайт')
    ->link('https://example.com')
    ->description('Новости')
    ->items($articles, fn ($a) => [
        'title'   => $a->title,
        'link'    => $a->url,
        'pubDate' => $a->published_at,
        'content' => $a->body,
    ])
    ->withValidation()
    ->build();

// Бросает ValidationException при первой ошибке (по умолчанию)
$builder->withValidation(throw: true)->build();

// Логирует ошибки, XML всё равно возвращается
$builder->withValidation(throw: false)->build();

// Только генерация, без валидации
$builder->withoutValidation()->build();

// Dry-run: получить ValidationResult без исключений
$result = $builder->validate();
if (!$result->isValid()) {
    print_r($result->errors());
}

use XmlFeeds\Builder\FeedBuilder;
use XmlFeeds\Platforms\TelegramChannel;

$xml = FeedBuilder::for(new TelegramChannel())
    ->title('Мой канал')
    ->link('https://t.me/mychannel')
    ->description('Лучшие новости')
    ->items($posts, fn ($p) => [
        'title'   => $p->title,    // обрежется до 255 символов
        'link'    => $p->url,
        'pubDate' => $p->created_at,
        'content' => $p->html,     // → content:encoded (CDATA)
        'enclosure' => ['url' => $p->image_url, 'type' => 'image/jpeg', 'length' => 0],
    ])
    ->build();

use XmlFeeds\Platforms\VkontakteFeed;

$xml = FeedBuilder::for(new VkontakteFeed())
    ->title('Новости')
    ->link('https://example.com')
    ->description('Последние публикации')
    ->items($articles, fn ($a) => [
        'title'   => $a->title,
        'link'    => $a->url,
        'pubDate' => $a->published_at,
        'content' => $a->body,
    ])
    ->build();

use XmlFeeds\Platforms\FacebookFeed;

$xml = FeedBuilder::for(new FacebookFeed())
    ->title('My Publication')
    ->link('https://example.com')
    ->description('Latest articles')
    ->items($articles, fn ($a) => [
        'title'   => $a->title,
        'link'    => $a->url,
        'pubDate' => $a->published_at,
        'content' => $a->html_body,  // обязательное поле для Instant Articles
    ])
    ->build();

FeedBuilder::for(new RssFeed())
    // ...
    ->withoutValidation()
    ->buildToFile('/var/www/feeds/news.xml');

$builder->generator($myGenerator);

$builder->schemaValidator($mySchemaValidator);

$builder->validator($myExtraValidator);

$builder->strictFields(true);

$builder->logger($psrLogger);

final class MyPlatform extends AbstractRssPlatform
{
    public function namespace(): array { return []; }
    public function  return null; }
    public function transform(FeedItem $item): array { return $item->toArray(); }
}

use XmlFeeds\Builder\FeedBuilder;
use XmlFeeds\Platforms\YandexNews;

$xml = FeedBuilder::for(new YandexNews())
    ->title('My site')
    ->link('https://example.com')
    ->description('News')
    ->items($articles, fn ($a) => [
        'title'   => $a->title,
        'link'    => $a->url,
        'pubDate' => $a->published_at,
        'content' => $a->body,
    ])
    ->withValidation()
    ->build();

$builder->withValidation(throw: true)->build();  // throws ValidationException on error
$builder->withValidation(throw: false)->build(); // logs errors, returns XML anyway
$builder->withoutValidation()->build();           // skip validation entirely

$result = $builder->validate(); // dry-run, returns ValidationResult