draliragab/socialize

轻松将社交媒体功能添加到您的Laravel项目

v1.7.0 2023-10-29 15:26 UTC

This package is auto-updated.

Last update: 2024-09-22 22:04:13 UTC


README

Latest Version on Packagist License

socialize 是一个帮助您轻松将社交媒体功能添加到Laravel项目的包。

您可以分享到 FacebookTwitterInstagram 等,更多功能即将推出。

$fb = Socialize::facebook()
        ->setMessage('Awesome message')
        ->setLink('https://github.com/')
        ->setAction(OgAction::FEELS, OgObject::EXCITED)
        ->sharePost();

dump($fb->getPostId()); // 123456789101112

Socialize

或者您可以使用模型特性来分享帖子。

$post = Post::first();
$response = $post->shareToFacebook();

echo $response->getPostId(); // 123456789101112

目录

安装

您可以通过 composer 安装此包

composer require draliragab/socialize

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="socialize-config"

这是已发布的配置文件的内容

return [
    'facebook' => [
        'default' => [
            'app_id' => env('FACEBOOK_APP_ID'),
            'graph_version' => env('FACEBOOK_GRAPH_VERSION', 'v15.0'),
            'page_id' => env('FACEBOOK_PAGE_ID'),
            'page_access_token' => env('FACEBOOK_PAGE_ACCESS_TOKEN'),
        ],
    ],

    'instagram' => [
        'default' => [
            'graph_version' => env('INSTAGRAM_GRAPH_VERSION', 'v15.0'),
            'user_access_token' => env('INSTAGRAM_USER_ACCESS_TOKEN'),
            'instagram_account_id' => env('INSTAGRAM_ACCOUNT_ID'),
        ],
    ],

    'twitter' => [
        'default' => [
            'app_consumer_key' => env('TWITTER_CONSUMER_KEY'),
            'app_consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
            'account_access_token' => env('TWITTER_ACCOUNT_ACCESS_TOKEN'),
            'account_access_token_secret' => env('TWITTER_ACCOUNT_ACCESS_TOKEN_SECRET'),
        ],
    ],

    'model_columns' => [
        'message_column' => 'title',
        'photo_column' => 'image',
    ],
];

配置

对于每个社交媒体,您需要将所需的凭证添加到您的 .env 文件或直接添加到已发布的配置文件中。

默认情况下,该包将使用 default 配置。但您可以通过添加更多配置来发布到多个页面和账户。

return [
    'facebook' => [
        'default' => [
            'app_id' => env('FACEBOOK_APP_ID'),
            'graph_version' => env('FACEBOOK_GRAPH_VERSION', 'v15.0'),
            'page_id' => env('FACEBOOK_PAGE_ID'),
            'page_access_token' => env('FACEBOOK_PAGE_ACCESS_TOKEN'),
        ],

        'page_2' => [
            'app_id' => env('FACEBOOK_APP_ID_2'),
            'graph_version' => env('FACEBOOK_GRAPH_VERSION', 'v15.0'),
            'page_id' => env('FACEBOOK_PAGE_ID_2'),
            'page_access_token' => env('FACEBOOK_PAGE_ACCESS_TOKEN_2'),
        ],
    ],
];

并将配置名称传递给初始化方法。

$fb = Socialize::facebook('page_2');

使用

所有社交媒体提供商都有 sharegetPostId 方法。

$fb = Socialize::facebook()
        ->share(
            message: 'Awesome message',
            image: 'https:example.com/image.jpg',
            options: [],
        );

$postId = $fb->getPostId();

Facebook

初始化

$fb = Socialize::facebook();

接受配置名称作为参数。

use DrAliRagab\Socialize\Socialize;

$fb = Socialize::facebook('page_2');

为 sharePost() 方法设置选项的方法

setBackdatedTime(float|Carbon $backdated_time)
setBackdatedTimeGranularity(string $backdated_time_granularity) // One of ['year', 'month', 'day', 'hour', 'minute']
addChildAttachment(string $link, string $name = null, string $description = null, string $image_hash = null, string $picture = null)
setFeedTargeting(int $age_max = null, int $age_min = null, array $college_years = null, array $education_statuses = null, array $genders = null, array $geo_locations = null, array $interests = null, int $locales = null, array $relationship_statuses = null)
setLink(string $link)
setMessage(string $text)
setMultiShareEndCard(bool $multi_share_end_card = true)
setMultiShareOptimized(bool $multi_share_optimized = true)
setObjectAttachment(string $object_attachment)
setPlace(string $placeId)
setPublished(bool $published = true)
setScheduledPublishTime(int|Carbon $scheduled_publish_time)
setTags(array $tagsIds)
setTargeting(int $age_min = null, array $geo_locations = null)
setAction(OgAction $action, OgObject $object, ?OgIcon $icon = null)
attachMedia(int $mediaId)
attachMedias(array $mediaIds)

// After setting the options you can share the post
sharePost()

// Also after sharing you can add comments to the post
addComment('Awesome comment')

// And you ca delete the post
deletePost(int $postId) // returns true if the post is deleted successfully

示例

use DrAliRagab\Socialize\Socialize;
use DrAliRagab\Socialize\Enums\OpenGraph\OgAction;
use DrAliRagab\Socialize\Enums\OpenGraph\OgObject;

$fb = Socialize::facebook();
$response = $fb
    ->setBackdatedTime(now()->subDays(2))
    ->setBackdatedTimeGranularity('day')
    ->addChildAttachment(
        link: 'https://example.com/1',
        name: "Awesome name",
        description: "Awesome description",
        picture: "https://example.com/image.jpg"
    )
    ->addChildAttachment(
        link: 'https://example.com/2',
        name: "Awesome name 2",
        description: "Awesome description 2",
        picture: "https://example.com/image2.jpg"
    )
    ->setFeedTargeting(
        age_max: 65,
        age_min: 18,
    )
    ->setLink('https://example.com')
    ->setMessage('Awesome message')
    ->setMultiShareEndCard(true)
    ->setMultiShareOptimized(true)
    ->setPublished(true)
    ->setTargeting(
        age_min: 18
    )
    ->setAction(OgAction::FEELS, OgObject::EXCITED)
    ->sharePost()   // Must be called after setting the options
    ->addComment('Awesome comment');    // Must be called after sharing the post

$postId = $response->getPostId();   // Get the post id

// Delete the post
$deleted = $fb->deletePost($postId);

uploadPhoto(), deletePhoto(), uploadVideo(), deleteVideo()

uploadPhoto(string $photoUrl, string $caption = null, bool $published = false, bool $temporary = true)

上传照片到页面。

$fb = Socialize::facebook();
$mediaId = $fb->uploadPhoto('https://example.com/image.jpg')->getMediaId();

// You can use the media id to attach it to a post
$postId = Socialize::facebook()
    ->attachMedia($mediaId)
    ->setMessage('Awesome image')
    ->sharePost()
    ->getPostId();

或者您可以直接将照片发布到页面。

$photoId = Socialize::facebook()
    ->uploadPhoto('https://example.com/image.jpg', 'Awesome image', true, false)
    ->getMediaId(); // returns the photo id

$deleted = Socialize::facebook()->deletePhoto($photoId); // returns true if the photo is deleted successfully

getPosts()

获取 Facebook 页面的帖子。返回 Collection

$data = Socialize::facebook()-getPosts();

接受两个参数

getTaggedPosts()

获取页面被标记的所有公开帖子。

getPost()

通过其 id 获取特定帖子。返回 Collection

接受两个参数

deletePost()

通过其 id 删除帖子。如果帖子成功删除,则返回 true

getComments()

获取帖子的评论。返回 Collection

接受三个参数

getUrl()

获取帖子的 URL。

Twitter

初始化

$twitter = Socialize::twitter();

接受配置名称作为参数。

use DrAliRagab\Socialize\Socialize;

$twitter = Socialize::twitter('account_2');

tweet()

向 Twitter 账户发布推文。

$twitter = Socialize::twitter();
$postId = $twitter->tweet('Awesome tweet')->getPostId();

设置 tweet 选项的方法

superFollowersOnly()
addPlace(string $placeId)
addPoll(array $pollOptions, int $pollDuration)
quoteTweet(string $tweetId)
restrictReply(string $restrictReply)    // "mentionedUsers" and "following" are the only options
inReplyTo(string $tweetId)
addMedia(array $mediaIds)
tagUsers(array $usernames)

示例

$postId = $twitter
    ->superFollowersOnly()
    ->addPoll(
        pollOptions: ['Disappointed 😞', 'Predictable 😐', 'Excited 😃'],
        pollDuration: 60,
    )
    ->quoteTweet('12345679101112')
    ->restrictReply('mentionedUsers')
    ->inReplyTo('12345679101112')
    ->tweet(
        text: 'https://example.com/',
    )->getPostId();

addComment()

向推文添加评论。

$postId = $twitter
    ->tweet('Awesome tweet')
    ->addComment('Awesome comment')
    ->getPostId();

uploadMedia(), getMediaIds()

将媒体上传到账户

接受一个包含媒体路径的 array

$imgPath = public_path('default-page-img.png');
$imgPath2 = public_path('default-page-img2.png');

$mediaIds = $twitter->uploadMedia([
    $imgPath,
    $imgPath2,
])->getMediaIds();

addMedia()

向推文添加媒体。

接受一个包含媒体 id 的 array

$postId = $twitter
    ->addMedia($mediaIds)
    ->tweet('Awesome tweet')->getPostId();

您可以组合 uploadMedia()addMedia()

$postId = $twitter
    ->uploadMedia([$imgPath])
    ->addMedia()
    ->tweet('Awesome tweet')
    ->getPostId();

deleteTweet()

通过其 id 删除推文。如果推文成功删除,则返回 true

Instagram

初始化

$insta = Socialize::instagram();

接受配置名称作为参数。

use DrAliRagab\Socialize\Socialize;

$insta = Socialize::instagram('account_2');

publishImage()

向 Instagram 账户发布图片。

接受三个参数

$insta = Socialize::instagram();
$postId = $insta->publishImage('https://example.com/image.jpg', 'Awesome image')->getPostId();

publishImageCarousel()

将图片轮播发布到Instagram账号。

接受三个参数

$postUrl = $insta
    ->publishImageCarousel([
        'https://example.com/image.jpg',
        'https://example.com/image2.jpg',
        'https://example.com/image3.jpg',
    ], 'Awesome image')
    ->addComment('Awesome image')
    ->getUrl();

addComment()

给帖子添加评论。

$postId = Socialize::instagram()
    ->publishImage('https://example.com/image.jpg', 'Awesome image')
    ->addComment('Awesome image', $postId)
    ->getPostId();

getUrl()

获取帖子的 URL。

$postUrl = Socialize::instagram()
    ->publishImage('https://example.com/image.jpg', 'Awesome image')
    ->getUrl();

getPost()

通过其 id 获取特定帖子。返回 Collection

接受两个参数

特性

Socializer

此特性用于从模型直接分享到社交媒体。

use DrAliRagab\Socialize\Traits\Socializer;

class Post extends Model
{
    use Socializer;
}

shareToFacebook(), shareToTwitter(), shareToInstagram()

从模型直接分享到社交媒体。

$post = Post::find(1);

$post->shareToFacebook();   // share to facebook
$post->shareToTwitter();    // share to twitter
$post->shareToInstagram();  // share to instagram

上述所有方法都在模型的image列中搜索图片,在title列中搜索消息。

您可以在config文件中更改列。

'model_columns' => [
    'message_column' => 'title',
    'photo_column' => 'image',
],

或者,您可以以数组的形式传递消息、图片和分享选项。

$post->shareToInstagram([
    'photo' => 'https://example.com/image.jpg',
    'message' => 'Awesome post',
    'options' => [
        'scheduled_publish_time' => now()->addDays(2)->timestamp,
    ],
]);

您还可以传递社交媒体账号。

$post->shareToTwitter([
    'photo' => public_path('default-page-img.png'),
    'message' => 'Awesome post',
    'config' => 'account_2',
]);

变更日志

请参阅 变更日志 了解最近的变化。

贡献

请参阅 贡献指南 获取详细信息。

安全漏洞

请查阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可证

MIT许可(MIT)。请参阅 许可文件 了解更多信息。