【问题标题】:Twitter API and duplicate tweetsTwitter API 和重复推文
【发布时间】:2017-07-17 22:18:02
【问题描述】:

我通过 CodeBird PHP 使用 twitter API 在网站上使用 Ajax 显示推文。我在第一次通话后无法防止重复,因为max_id 在包含 max_id 之后显示推文。

对我来说,他们为什么要这样做似乎很愚蠢,但我似乎也找不到其他人问同样的问题。我认为这将是很多人遇到的事情。这让我觉得我做的不对。

    // Prepare Args
    $tw_args = array(
        'count' => 5,
        'trim_user' => true
    );

    // If we havea last id, set it
    if (isset($last_id)) {
        $tw_args['max_id'] = $last_id;
    }

    // new tweet arr
    $tweets = array();

    // get raw tweets
    $raw_tweets = (array) $cb->statuses_userTimeline($tw_args);

    // loop through tweets
    for ($i = 0; $i < count($raw_tweets); $i++) { 

        // Add just the ID and text to our new tweets array
        $tmp = array(
            'id' => $raw_tweets[$i]->id_str,
            'text' => $raw_tweets[$i]->text
        );

        array_push($tweets, $tmp);
    }


    // return tweets array
    return $tweets;

如何防止 twitter API 返回与 max_id 具有相同 id 的推文?

【问题讨论】:

    标签: php twitter


    【解决方案1】:

    如果我很好理解您要做什么,max_id 推文似乎包含在 $raw_tweets 数组中?

    如果是这样,只需在循环中添加一个条件,以不将带有 max_id 作为 id 的推文添加到 $tweets 数组:

        // ...
    
        for ($i = 0; $i < count($raw_tweets); $i++) {
    
            // Check if the current tweet's id match max_id
            if($raw_tweets[$i]->id_str == max_id)
                continue;
    
            // Add just the ID and text to our new tweets array
            $tmp = array(
                'id' => $raw_tweets[$i]->id_str,
                'text' => $raw_tweets[$i]->text
            );
    
            array_push($tweets, $tmp);
        }
    
        // ...
    

    【讨论】:

    • 这就是我所采用的方法,但似乎每次通话我都会抓取一条比我需要的推文更多的推文,因为我只会再次删除它。我认为可能有更好的方法...
    • 根据twitter api,没有办法在api响应中不包含max_id tweet..
    【解决方案2】:

    如果您的平台能够处理 64 位整数,只需从您确定为 max_id 的任何值中减去 1。根据 API 文档here,“调整后的 max_id 是否是有效的推文 ID,或者它是否对应于不同用户发布的推文并不重要——该值仅用于决定过滤哪些推文。”这将防止收到任何重复。

    但是,如果您无法处理 64 位整数,文档会说跳过这一步,所以我认为没有比 ZeusInTexas 提供的更简单的解决方法了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-05
      • 2023-03-31
      • 2011-02-11
      • 1970-01-01
      • 2013-06-10
      • 2011-10-20
      • 2011-05-05
      • 1970-01-01
      相关资源
      最近更新 更多