PHP code example of treehouselabs / feeder

1. Go to this page and download the library: Download treehouselabs/feeder 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/ */

    

treehouselabs / feeder example snippets


// create a new reader, in this case we'll fetch a feed from the interwebs
$resource = new FileResource(HttpTransport::create('http://example.org/feed'));
$reader = new XmlReader($resource);

// tell the reader to pull <item> nodes
$reader->setNodeCallback('item');

// create a feed for this reader
$feed = new Feed($reader);

// now simply iterate over the items
foreach ($feed as $item) {
    // $item is a ParameterBag instance with the serialized <item> node as data
}

[
  'title' => 'The quick brown fox jumps over the lazy dog',
  'publishDate' => 'Thu, 05 Mar 2015 20:24:38 +0000',
  'explicit' => 'yes',
  'link' => [
    '#'     => '',
    '@href' => 'http://example.org/articles/1'
  ],
]

$feed->addTransformer(new LowercaseKeysTransformer());

// will return:
[
  'title' => 'The quick brown fox jumps over the lazy dog',
  'publish_date' => 'Thu, 05 Mar 2015 20:24:38 +0000',
  'explicit' => 'yes',
  'link' => [
    '#'     => '',
    '@href' => 'http://example.org/articles/1'
  ],
]

// the DataTransformer wraps a transformer for a specific field,
// instead of the whole item
$transformer = new DataTransformer(
    new StringToDateTimeTransformer(DATE_RFC2822),
    'publish_date'
);
$feed->addTransformer($transformer);

// will return:
[
  'title' => 'The quick brown fox jumps over the lazy dog',
  'publish_date' => DateTime::__set_state(array(
    'date' => '2015-03-05 20:24:38.000000',
    'timezone_type' => 1,
    'timezone' => '+00:00',
  )),
  'explicit' => 'yes',
  'link' => [
    '#'     => '',
    '@href' => 'http://example.org/articles/1'
  ],
]

$feed->addTransformer(
  new DataTransformer(
    new StringToBooleanTransformer(),
    'explicit'
  )
);
$feed->addTransformer(
    new DataTransformer(
        new CallbackTransformer(function ($value) { return $value['@href']; }),
        'link'
    )
);

// will return:
[
  'title' => 'The quick brown fox jumps over the lazy dog',
  'publish_date' => DateTime::__set_state(array(
    'date' => '2015-03-05 20:24:38.000000',
    'timezone_type' => 1,
    'timezone' => '+00:00',
  )),
  'explicit' => true,
  'link' => 'http://example.org/articles/1',
]