Laravel 以用户身份发送推文
Laravel send tweet as user
是否可以像用户一样从 laravel 发送自动推文。
用户必须由我的推特API登录,然后我可以用他的身份发推文吗?当然他必须接受这个动作。我必须知道用户何时发布我的页面。
它可以像 facebook share 一样工作吗?有这方面的工具吗?
对不起我的英语。
谢谢大家的回答。
PS。
我不是说这个包 http://vegibit.com/send-a-tweet-with-laravel/ 。
我需要向用户 table.
发送推文
使用这个 https://twitteroauth.com/ 包。
推入你的控制台composer require abraham/twitteroauth
全部安装后,您应该添加
use Abraham\TwitterOAuth\TwitterOAuth;
到你的 class.
现在在功能中,您必须与 api.
建立连接
$connection = new TwitterOAuth(
CONSUMER_KEY, // Information about your twitter API
CONSUMER_SECRET, // Information about your twitter API
$access_token, // You get token from user, when him sigin to your app by twitter api
$access_token_secret// You get tokenSecret from user, when him sigin to your app by twitter api
);
如果您创建了连接,那么您可以作为用户发送 post。
例如:
$connection->post("statuses/update", ["status" => "My first post!"]);
我发现 ThuJohn 包是这类工作的赢家。
https://github.com/thujohn/twitter
易于使用各种选项,例如
在没有媒体的情况下实时发送推文:
Route::get('/send-tweet-no-media', function()
{
return Twitter::postTweet(['status' => 'Laravel is beautiful', 'format' => 'json']);
});
使用媒体实时发送推文:
Route::get('/send-tweet-with-media', function()
{
$uploaded_media = Twitter::uploadMedia(['media' => File::get(public_path('filename.jpg'))]);
return Twitter::postTweet(['status' => 'Laravel is beautiful', 'media_ids' => $uploaded_media->media_id_string]);
});
您还可以做各种其他事情,例如登录,您可以将其提升一个层次,轻松地向您的用户存储用户令牌和密码 table 并执行预定的帖子。
一旦您存储了用户令牌和密码,您就可以像这样为他们安排发布的推文:
//Get the Users token & from your User Table (or where ever you stored them)
$token = $user->token;
$secret = $user->secret;
//This line resets the token & secret with the users
Twitter::reconfig(['token' => $token, 'secret' => $secret]);
//This line posts the tweet as the user
Twitter::postTweet(['status' => 'test', 'format' => 'json']);
是否可以像用户一样从 laravel 发送自动推文。 用户必须由我的推特API登录,然后我可以用他的身份发推文吗?当然他必须接受这个动作。我必须知道用户何时发布我的页面。 它可以像 facebook share 一样工作吗?有这方面的工具吗?
对不起我的英语。 谢谢大家的回答。
PS。 我不是说这个包 http://vegibit.com/send-a-tweet-with-laravel/ 。 我需要向用户 table.
发送推文使用这个 https://twitteroauth.com/ 包。
推入你的控制台composer require abraham/twitteroauth
全部安装后,您应该添加
use Abraham\TwitterOAuth\TwitterOAuth;
到你的 class.
现在在功能中,您必须与 api.
建立连接$connection = new TwitterOAuth(
CONSUMER_KEY, // Information about your twitter API
CONSUMER_SECRET, // Information about your twitter API
$access_token, // You get token from user, when him sigin to your app by twitter api
$access_token_secret// You get tokenSecret from user, when him sigin to your app by twitter api
);
如果您创建了连接,那么您可以作为用户发送 post。 例如:
$connection->post("statuses/update", ["status" => "My first post!"]);
我发现 ThuJohn 包是这类工作的赢家。 https://github.com/thujohn/twitter 易于使用各种选项,例如
在没有媒体的情况下实时发送推文:
Route::get('/send-tweet-no-media', function()
{
return Twitter::postTweet(['status' => 'Laravel is beautiful', 'format' => 'json']);
});
使用媒体实时发送推文:
Route::get('/send-tweet-with-media', function()
{
$uploaded_media = Twitter::uploadMedia(['media' => File::get(public_path('filename.jpg'))]);
return Twitter::postTweet(['status' => 'Laravel is beautiful', 'media_ids' => $uploaded_media->media_id_string]);
});
您还可以做各种其他事情,例如登录,您可以将其提升一个层次,轻松地向您的用户存储用户令牌和密码 table 并执行预定的帖子。
一旦您存储了用户令牌和密码,您就可以像这样为他们安排发布的推文:
//Get the Users token & from your User Table (or where ever you stored them)
$token = $user->token;
$secret = $user->secret;
//This line resets the token & secret with the users
Twitter::reconfig(['token' => $token, 'secret' => $secret]);
//This line posts the tweet as the user
Twitter::postTweet(['status' => 'test', 'format' => 'json']);