mpratt/simple-lifestream

一个从多个社交网站/服务中返回生活流事件的库。

4.7.4 2015-05-30 05:04 UTC

This package is auto-updated.

Last update: 2024-09-11 14:42:33 UTC


README

Build Status Scrutinizer Quality Score Code Coverage Latest Stable Version Total Downloads

一个非常简单灵活的库,用于您的生活流目的。它支持许多第三方提供商,并使您能够在一个地方显示所有这些信息。

这个库的优点是它只返回一个包含所有重要数据(日期、html等)的数组。这使您能够随意处理这些信息并以您喜欢的方式显示。还有一些格式化工具,您可以使用它们以任何您喜欢的方式输出数据,下面有示例供您参考。

为了获得良好的性能并避免向其他站点发送过多请求,该库内部使用基于文件的缓存系统(文件缓存)。每个缓存的默认持续时间是10分钟,但您可以轻松地修改这种行为。

这个库的名字灵感来源于那部关于Paris Hilton和Nicole Ritchie的旧廉价真人秀。

支持的网站

  • DaliyMotion
  • Delicious
  • Deviantart
  • Dribble
  • FacebookPages
  • Atom/RSS Feeds
  • Github
  • GimmeBar
  • Reddit
  • StackExchange/StackOverflow
  • Twitter (重要!您必须先注册应用)
  • Instagram (重要!您必须先注册应用)
  • Pinboard (重要!您需要有一个API令牌。您可以在设置页面找到您的令牌)
  • Youtube

有关每个提供者的更详细信息的文件,请参阅STREAMS.md文件。

请记住,Atom/RSS提供者非常有用,可以获取提供RSS/Atom用户动作feed的网站的动作,例如Vimeo、Flickr、LastFM、WordPress博客、Blogger博客等。

因此,从理论上讲,还有许多其他网站可以用来增强您的生活流。

要求

  • PHP >= 5.3
  • Curl或allow_url_fopen必须启用

安装

使用Composer安装

如果您使用Composer来管理依赖项,您可以通过创建composer.json并添加以下内容来使用此库:

{
    "require": {
        "mpratt/simple-lifestream": "~4.0"
    }
}

保存并运行composer.phar install

独立安装(不使用Composer)

下载最新版本或克隆此存储库,将Lib/SimpleLifestream目录放置在您的项目中。之后,您只需包含Autoload.php文件。

    require '/path/to/SimpleLifestream/Autoload.php';
    $lifestream = new \SimpleLifestream\SimpleLifestream();

或者如果您已经有了PSR-0遵守的自动加载器,您只需注册该库

    $loader->registerNamespace('SimpleLifestream', 'path/to/SimpleLifestream');

基本用法

创建一个包含有效流提供者的数组,并将其传递给SimpleLifestream对象。

    $streams = array(
        new \SimpleLifestream\Stream('Reddit', 'mpratt'),
        new \SimpleLifestream\Stream('Github', 'mpratt'),
        new \SimpleLifestream\Stream('Youtube', 'ERB'),
        new \SimpleLifestream\Stream('StackOverflow', '430087'),
        new \SimpleLifestream\Stream('FacebookPages', '27469195051'),
        new \SimpleLifestream\Stream('Feed', 'http://www.michael-pratt/blog/rss/'),
    );

    $lifestream = new \SimpleLifestream\SimpleLifestream();
    $lifestream->loadStreams($streams);

    $data = $lifestream->getLifestream();
    foreach ($data as $d) {
        echo $d['html'];
    }

getLifestream(int 0)方法接受一个数字,可以用来限制您想要获取的最新信息。

    $data = $lifestream->getLifestream(10);
    echo count($data); // 10

配置指令

SimpleLifestream构造函数接受一个配置指令数组,您可以使用它来修改库的一些部分。

    $config = array(
        'date_format' => 'Y-m-d H:i', // Date format returned on by the streams
        'link_format' => '<a href="{url}">{text}</a>', // Link template used by the streams
        'language' => 'English', // The Output language
        'cache_ttl' => (60*10), // Optional: Duration of the cache in seconds
        'cache_dir' => '/path/bla/bla', // Optional: A place where the cache should be stored
    );

    $lifestream = new \SimpleLifestream\SimpleLifestream($config);

本库支持英语和西班牙语。如果您希望输出结果为西班牙语,只需写下

    $config = array(
        'language' => 'Spanish',
    );

    $streams = array(
        new \SimpleLifestream\Stream('Reddit', 'mpratt'),
        new \SimpleLifestream\Stream('Github', 'mpratt'),
    );

    $lifestream = new \SimpleLifestream\SimpleLifestream($config);
    $data = $lifestream->loadStreams($streams)->getLifestream();

    foreach ($data as $d) {
        echo $d['html'];
    }

流配置

\SimpleLifestream\Stream 对象需要两个参数。第一个是一个包含提供者名称的字符串。当提供无效的提供者时,将抛出 InvalidArgumentException

第二个参数可以是一个包含相关资源/URL/用户名的字符串,或者是一个包含重要配置选项的数组。注册流的常规方式是

    $streams = array(
        new \SimpleLifestream\Stream('Github', 'mpratt'),
        new \SimpleLifestream\Stream('Youtube', 'ERB'),
    );

或者使用带有 resource 键的关联数组

    $streams = array(
        new \SimpleLifestream\Stream('Github', array('resource' => 'mpratt')),
        new \SimpleLifestream\Stream('Youtube', array('resource' => 'ERB')),
    );

resource 键在内部使用,并解释为提供者所需的相关用户名/URL/用户ID。

话虽如此,某些流需要额外的信息才能工作,让我们看看 Twitter 提供者。记住,如果您想使用 Twitter 提供者,首先您必须注册一个应用

    $streams = array(
        new \SimpleLifestream\Stream('twitter', array(
            'consumer_key'    => 'your consumer key',
            'consumer_secret' => 'your consumer secret',
            'access_token' => 'you access token',
            'access_token_secret' => 'your access token secret',
            'resource' => 'your twitter username',
        ))
    );

    $lifestream = new \SimpleLifestream\SimpleLifestream();
    $output = $lifestream->loadStreams($streams)->getLifestream();
    print_r($output);

您可以在少数提供者上使用此技术以修改它们的行为。另一个例子可能是 StackExchange 提供者。该提供者使您能够访问 StackExchange 网络中的所有站点,而不仅仅是 StackOverflow。例如,如果我们想从 http://programmers.stackexchange.com 获取一个用户的数据。

    $streams = array(
        new \SimpleLifestream\Stream('StackExchange', array(
            'site' => 'programmers',
            'resource' => '430087',
        ))
    );

    $lifestream = new \SimpleLifestream\SimpleLifestream();
    $output = $lifestream->loadStreams($streams)->getLifestream();
    print_r($output);

有关流及其各个配置选项的更多信息,请阅读 STREAMS.md 文件。

高级用法

错误检查

有三种错误检查方法:bool hasErrors()array getErrors()string getLastError()

    $data = $lifestream->getLifestream();
    if ($lifestream->hasErrors()) {
        echo $lifestream->getLastError();
    }

    if ($lifestream->hasErrors()) {
        var_dump($lifestream->getErrors());
    }

访问原始响应(添加自己的键)

从版本 4.7.0 开始,您可以从提供者访问原始响应并返回自定义键以供您自己的使用。您可以根据提供者修改响应。

    $streams = array(
        new \SimpleLifestream\Stream('Reddit', array(
            'resource' => 'mpratt',
            'callback' => function ($value) {
                return array(
                    'modified_title' => str_replace(' ', '-', $value['data']['title'])
                );
            },
        )),
        new \SimpleLifestream\Stream('Github', 'mpratt'),
        new \SimpleLifestream\Stream('Youtube', 'ERB'),
    );

    $lifestream = new \SimpleLifestream\SimpleLifestream();
    $output = $lifestream->loadStreams($streams)->getLifestream();
    print_r($output);

或者,您可以通过每个流中 addCallback() 方法的使用来注册回调。

    $reddit = new \SimpleLifestream\Stream('Reddit', 'mpratt');
    $reddit->addCallback(function ($v) {
        return array(
            'modified_title' => str_replace(' ', '', $v['data']['title'])
        );
    });

    $streams = array(
        $reddit,
        new \SimpleLifestream\Stream('Github', 'mpratt'),
        new \SimpleLifestream\Stream('Youtube', 'ERB'),
    );

    $lifestream = new \SimpleLifestream\SimpleLifestream();
    $output = $lifestream->loadStreams($streams)->getLifestream();
    print_r($output);

忽略动作/类型

如您所见,某些服务检测多个动作,但在某些情况下,您可能不希望拥有所有这些信息。您可以使用 ignore() 方法忽略具体的动作。

    // Tell the library to Ignore all favorited actions/types
    $lifestream->ignore('favorited');

    $data = $lifestream->getLifestream();

或者,您可以限制动作到特定的流提供者

    // Tell the library to Ignore all starred actions/types only from the Github Provider
    $lifestream->ignore('favorited', 'Github');

    $data = $lifestream->getLifestream();

输出格式化

让我们谈谈输出格式化器。有两种格式化器(HtmlListTemplate),可以帮助您以不同的方式显示数据。

为了使用它们,您必须应用装饰器模式。这样做时,getLifestream() 方法被转换,并且它不再返回包含信息的数组,而是返回一个包含请求数据的 字符串,该字符串位于模板中。

让我们看看 HtmlList 装饰器

    <?php
        $config = array();

        $streams = array(
            new \SimpleLifestream\Stream('Reddit', 'mpratt')
        );

        $lifestream = new \SimpleLifestream\SimpleLifestream($config);
        $lifestream = new \SimpleLifestream\Formatters\HtmlList($lifestream);
        $lifestream->loadStreams($streams);
        echo $lifestream->getLifestream(4);

        /* This prints something around this lines:
           <ul class="simplelifestream">
            <li class="servicename">Y-m-d H:i - <a href="...">text 1</a></li>
            <li class="servicename">Y-m-d H:i - <a href="...">text 2</a></li>
            <li class="servicename">Y-m-d H:i - <a href="...">text 3</a></li>
            <li class="servicename">Y-m-d H:i - <a href="...">text 4</a></li>
           </ul>
        */
    ?>

另一种装饰器称为 Template,它更加灵活,您可以使用它来定义自己的模板,并借助一些占位符,您可以使用库获取的数据进行插值。

    <?php
        $lifestream = new \SimpleLifestream\Formatters\Template(new \SimpleLifestream\SimpleLifestream());
        $lifestream->setTemplate('<div class="{service}">{text} {link}</div>');
        echo $lifestream->loadStreams($streams)->getLifestream();

        /* This prints something round this lines:
            <div class="servicename">a text <a href="..">a link</a></div>
            <div class="servicename">another text <a href="..">a link</a></div>
            <div class="servicename">and more text <a href="..">a link</a></div>
        */
    ?>

如果您想看到更多关于如何使用此库的示例,请查看 Tests 目录中的文件。否则,检查库的源代码,我会说它有“相当好”的英文文档,应该很容易理解。

许可

MIT

有关完整的版权和许可信息,请查看 LICENSE 文件。

作者

Michael Pratt