draliragab / socialize
轻松将社交媒体功能添加到您的Laravel项目
Requires
- php: ^8.0
- abraham/twitteroauth: ^6.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpcompatibility/php-compatibility: *
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10
README
socialize 是一个帮助您轻松将社交媒体功能添加到Laravel项目的包。
您可以分享到 Facebook、Twitter、Instagram 等,更多功能即将推出。
$fb = Socialize::facebook() ->setMessage('Awesome message') ->setLink('https://github.com/') ->setAction(OgAction::FEELS, OgObject::EXCITED) ->sharePost(); dump($fb->getPostId()); // 123456789101112
或者您可以使用模型特性来分享帖子。
$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');
使用
所有社交媒体提供商都有 share
和 getPostId
方法。
$fb = Socialize::facebook() ->share( message: 'Awesome message', image: 'https:example.com/image.jpg', options: [], ); $postId = $fb->getPostId();
初始化
$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();
接受两个参数
$limit
返回的最大帖子数。默认为 25,最大为 100。$fields
返回的字段。请参阅 https://developers.facebook.com/docs/graph-api/reference/page/feed#readfields
getTaggedPosts()
获取页面被标记的所有公开帖子。
getPost()
通过其 id 获取特定帖子。返回 Collection
。
接受两个参数
$postId
帖子 id$fields
返回的字段。请参阅 https://developers.facebook.com/docs/graph-api/reference/pagepost#fields
deletePost()
通过其 id 删除帖子。如果帖子成功删除,则返回 true
。
getComments()
获取帖子的评论。返回 Collection
。
接受三个参数
$postId
帖子 id$limit
返回的最大评论数。默认为 25,最大为 100。$fields
返回的字段。请参阅 https://developers.facebook.com/docs/graph-api/reference/pagepost#fields
getUrl()
获取帖子的 URL。
初始化
$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
。
初始化
$insta = Socialize::instagram();
接受配置名称作为参数。
use DrAliRagab\Socialize\Socialize; $insta = Socialize::instagram('account_2');
publishImage()
向 Instagram 账户发布图片。
接受三个参数
$imageUrl
图片 URL$caption
图片的标题$options
选项数组。请参阅 https://developers.facebook.com/docs/instagram-api/reference/ig-user/media#query-string-parameters
$insta = Socialize::instagram(); $postId = $insta->publishImage('https://example.com/image.jpg', 'Awesome image')->getPostId();
publishImageCarousel()
将图片轮播发布到Instagram账号。
接受三个参数
$imageUrls
图片URL数组$caption
图片的标题$options
选项数组。请参阅 https://developers.facebook.com/docs/instagram-api/reference/ig-user/media#query-string-parameters
$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
。
接受两个参数
$postId
帖子 id$fields
要返回的字段。请参阅 https://developers.facebook.com/docs/instagram-api/reference/ig-media#fields
特性
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)。请参阅 许可文件 了解更多信息。