【问题标题】:Laravel send tweet as userLaravel 以用户身份发送推文
【发布时间】:2017-03-23 09:49:02
【问题描述】:

是否可以像用户一样从 laravel 发送自动推文。 用户必须通过我的 twitter API 登录,然后我才能以他的身份发送推文?当然他必须接受这个动作。我必须知道用户何时发布我的页面。 它可以像facebook分享一样工作吗?有这方面的工具吗?

对不起我的英语。 谢谢大家的回答。

附言。 我不是说这个包http://vegibit.com/send-a-tweet-with-laravel/。 我需要向用户表发送推文。

【问题讨论】:

  • 您可以使用api高音分享消息。但是,如果您想在自己的数据库中保留日期用户,则需要手动添加(模型、控制器、迁移等)。
  • Codebird libray for PHP by Jublo 是一个很好的库,可以在您的 php 站点中实现 Twitter 登录和其他功能。看看下面的链接github.com/jublonet/codebird-php

标签: php laravel twitter send tweets


【解决方案1】:

使用这个https://twitteroauth.com/ 包。

推入你的控制台composer require abraham/twitteroauth 全部安装后,您应该添加 use Abraham\TwitterOAuth\TwitterOAuth;你的班级。

现在在功能中,您必须创建与您的 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
);

如果您创建了连接,那么您可以作为用户发送帖子。 例如:

$connection->post("statuses/update", ["status" => "My first post!"]);

【讨论】:

    【解决方案2】:

    我发现 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]);
    });
    

    您还可以执行各种其他操作,例如登录,您可以更进一步,轻松地将用户令牌和密码存储到您的用户表中并执行预定的帖子。

    一旦您存储了用户令牌和秘密,您就可以像这样为他们预定发布的推文:

    //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']);       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-07
      • 2011-01-16
      • 2021-10-07
      • 2015-08-04
      • 2017-08-19
      • 2018-01-24
      • 2020-06-23
      相关资源
      最近更新 更多