【问题标题】:How to fetch realtime tweets posted by particular twitter account / user?如何获取特定 Twitter 帐户/用户发布的实时推文?
【发布时间】:2018-02-25 06:05:55
【问题描述】:

以下代码以数组格式为特定 Twitter 帐户/用户提供所有推文

require_once('TwitterAPIExchange.php');
$settings = array(
    'oauth_access_token' => "your twitter account access_token",
    'oauth_access_token_secret' => "your twitter access_token_secret",
    'consumer_key' => "your twitter consumer_key",
    'consumer_secret' => "your twitter consumer_secret"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$requestMethod = 'GET';

$getfield = '?q=+from:test_account';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(),$assoc = TRUE);
    if($string["errors"][0]["message"] != "") { echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
    echo "<pre>";
        print_r($string);
    echo "</pre>";.

但我想做以下两件事。

  1. 每当我点击/打开我网站上的任何 Twitter 帐户页面时,它都会为该 Twitter 帐户提供实时推文。意味着每当发布该帐户的新推文时,它们都应立即显示在我的网站上。

  2. 目前我需要为每个 Twitter 帐户生成 oAuth 数据(访问令牌、访问令牌秘密、消费者密钥、消费者秘密)。那么有什么方法可以让我获取特定 Twitter 帐户的推文并将其显示在我的网站上,而不会产生上述 4 个提到的值?

    谢谢你,

【问题讨论】:

    标签: php twitter oauth


    【解决方案1】:

    首先,您不必为用户制作新应用。 其次,您使用的 url 只是为了访问您的时间线,以便您可以从您的帐户中检索推文。仅从用户的推文中搜索,您将需要他们的屏幕名称,如@name。

    试试这个,

    $url = 'https://api.twitter.com/1.1/search/tweets.json';
    $requestMethod = 'GET';
    $getfield = '?q=from:test_account';
    
    $twitter = new TwitterAPIExchange($settings);
    $tweet =  $twitter->setGetfield($getfield)
                     ->buildOauth($url, $requestMethod)
                     ->performRequest();
    $tweets = json_decode($tweet,1);
    

    按照文档中的建议,这里 https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2016-06-25
    • 2012-07-24
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2018-10-02
    相关资源
    最近更新 更多