如何使用Twitter API获取特定数量的推文

时间:2019-06-06 04:57:04

标签: php twitter

我正在开发一个新的网站,我想在我的帐户中显示最新的5条帖子。但是我不知道如何设置限制并获得结果。

<?php
require_once('TwitterAPIExchange.php');
require_once('TwitterTextFormatter.php');
use Netgloo\TwitterTextFormatter;

$settings = array(
  'consumer_key' => '',
  'consumer_secret' => '',
  'oauth_access_token' => '',
  'oauth_access_token_secret' => '',
);

$screen_name = 'twitter';
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = "?screen_name={$screen_name}";
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$user_timeline = $twitter
  ->setGetfield($getfield)
  ->buildOauth($url, $requestMethod)
  ->performRequest();

$user_timeline = json_decode($user_timeline);
echo "<ul>";
foreach ($user_timeline as $user_tweet) {
  echo "<li>";
  echo TwitterTextFormatter::format_text($user_tweet) . "<br/>";
  if (isset($user_tweet->entities->media)) {
    $media_url = $user_tweet->entities->media[0]->media_url;
//    echo "<img src='{$media_url}' width='150px' />";
  }

  echo "</li>";
}
echo "</ul>";
?>

0 个答案:

没有答案