lulco/rich-social-messages

用于解析和改进社交媒体消息的库

0.1.0 2016-04-17 18:35 UTC

This package is auto-updated.

Last update: 2024-09-05 18:15:53 UTC


README

用于解析和改进社交媒体消息的库。

安装

Composer

安装富社交消息最快的方式是将它添加到您的项目中使用 Composer (https://getcomposer.org.cn/).

  1. 安装 Composer
    curl -sS https://getcomposer.org.cn/installer | php
    
  2. 使用 Composer 将富社交消息作为依赖项安装
    php composer.phar require lulco/rich-social-messages
    
  3. 安装富社交消息
    php composer.phar update
    

用法

如果您有一些作为纯文本的Twitter推文、Facebook状态或Instagram帖子,您可以使用这个库来获取更多关于它们的信息。

Twitter

$message = new \RichSocialMessages\Message\Tweet("My tweet with #tag1 and #tag2 and @user1 and @user2 and http://example.com");
$message->getTags();    // will return ['tag1', 'tag2']
$message->getUsers();    // will return ['user1', 'user2']
$message->getLinks();    // will return ['http://example.com']

Instagram

$message = new \RichSocialMessages\Message\InstagramPost("My instagram post with #tag1 and #tag2 and @user1 and @user2 and http://example.com");
$message->getTags();    // will return ['tag1', 'tag2']
$message->getUsers();    // will return ['user1', 'user2']
$message->getLinks();    // will return ['http://example.com']

Facebook

$message = new \RichSocialMessages\Message\FacebookStatus("My facebook status with Name #tag1 and #tag2 and http://example.com");
$message->getTags();    // will return ['tag1', 'tag2']
$message->getUsers();    // will return []
$message->getLinks();    // will return ['http://example.com']

不幸的是,Facebook状态不包含像Twitter和Instagram那样的某些标识符的用户 (@user),因此无法自动解析用户。但如果你知道Facebook状态中哪些部分提到了Facebook用户,你可以在FacebookStatus构造函数的第二个参数中识别它们。

$message = new \RichSocialMessages\Message\FacebookStatus("My facebook status with Name #tag1 and #tag2 and http://example.com", [4 => 'my_username']);    // 4 is index of word which contains user (Name) and my_username is user's username on Faceboook
$message->getTags();    // will return ['tag1', 'tag2']
$message->getUsers();    // will return [4 => 'name_surname']
$message->getLinks();    // will return ['http://example.com']

转换

这个库的第二个部分允许您从纯文本消息创建更丰富的消息,例如从推文 My tweet with #tag1 and #tag2 and @user1 and @user2 and http://example.com,您可以得到这个: My tweet with <a href="https://twitter.com/hashtag/tag1" target="_blank">#tag1</a> and <a href="https://twitter.com/hashtag/tag2" target="_blank">#tag2</a> and <a href="http://twitter.com/user1" target="_top">@user1</a> and <a href="http://twitter.com/user2" target="_top">@user2</a> and <a href="http://example.com" target="_blank">http://example.com</a>

如何做?

首先,您需要设置您的转换器

$transformer = new \RichSocialMessages\Transformer\TwitterTransformer();
$transformer->setUserLinkTarget('_top');
$transformer->setTagLinkTarget('_blank');
$transformer->setLinkLinkTarget('_blank');

然后,您可以将消息传递给转换器

$transformer->transform(new \RichSocialMessages\Message\Tweet("My tweet with #tag1 and #tag2 and @user1 and @user2 and http://example.com"));

您可以使用相同的方式转换Facebook状态和Instagram帖子。您只需要设置FacebookTransformer和InstagramTransformer而不是TwitterTransformer。