【发布时间】:2018-10-31 19:48:14
【问题描述】:
我已集成 Twitter API (Twitter OAuth) 以获取特定公司帐户的最新提要,下面是我迄今为止所做的代码 (https://tomelliott.com/php/authenticating-twitter-feed-timeline-oauth)。
<?php
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$twitteruser = "CompanyName";
$notweets = 3;
$consumerkey = "xxxxxxxx";
$consumersecret = "xxxxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxxxx";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret)
{
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser . "&count=" . $notweets);
?>
<?php foreach ($tweets as $current_tweet) { ?>
<div class="card">
<div class="card-body">
<div class="media">
<div class="media-body">
<h5 class="F-20 themeFontGrey MontSemBold text-uppercase">REGENCY CORPORATE</h5>
<p class="MontRegular themeFontGrey">
<?php
$date = $current_tweet->created_at;
echo date("F d Y, H:i A", strtotime($date));
?>
</p>
</div>
<?php
$twitt_url = '#';
$twitter_target = '';
if (!empty($current_tweet->id)) {
$twitt_url = 'https://twitter.com/' . $twitteruser . '/status/' . $current_tweet->id;
$twitter_target = 'target="_blank"';
}
?>
<a href="<?php echo $twitt_url; ?>" class="hovicon effect-5 news-icon" <?php echo $twitter_target; ?> >
<i class="fa fa-twitter"></i>
</a>
</div>
<p class="MontRegular themeFontGrey">
<?php echo $current_tweet->text; ?>
</p>
</div>
<?php if (!empty($current_tweet->entities->media[0]->media_url)) { ?>
<div class="newsImages">
<img src="<?php echo $current_tweet->entities->media[0]->media_url; ?>" alt="Images" height="20%" width="20%" />
</div>
<?php
} ?>
<hr />
</div>
<?php
} ?>
这很好用,我收到了 3 条最新推文。现在我想在其中添加分页,因此我遵循了 Twitter (https://developer.twitter.com/en/docs/basics/cursoring.html) 提供的文档,下面是我更新的代码与 cursor 相同,我打印了数组(响应)。
<?php
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$twitteruser = "CompanyName";
$notweets = 3;
$cursor = -1;
$consumerkey = "xxxxxxxx";
$consumersecret = "xxxxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxxxx";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret)
{
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser . "&count=" . $notweets . "&cursor=" . $cursor);
echo '<pre>';
print_r($tweets);
exit;
?>
如您所见,这里我添加了$cursor = -1; 并将我的api 目标url 更新为$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser . "&count=" . $notweets . "&cursor=" . $cursor);,传递cursor 值。
在这里,我收到了 3 条最近的推文,但是,根据上述链接 (https://developer.twitter.com/en/docs/basics/cursoring.html) 的文档中所述,您应该得到如下回复。
{
"ids": [
385752029,
602890434,
...
333181469,
333165023
],
"next_cursor": 1374004777531007833,
"next_cursor_str": "1374004777531007833",
"previous_cursor": 0,
"previous_cursor_str": "0"
}
我也尝试将请求的提要网址更新为此。
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser . "&cursor=" . $cursor);
但到目前为止,我没有以任何方式获得像 next_cursor 这样的任何密钥来继续操作。有人可以指导我,我在这里做错了什么,我应该怎么做才能从这里开始分页?
任何帮助或建议将不胜感激。
谢谢
【问题讨论】:
-
[URL]&page=2&count=" . $notweets 试试这个
-
@DsRaj 它以这种方式工作。但我想在此使用
cursoring。 -
看来
statuses/user_timeline不支持游标。查看文档:developer.twitter.com/en/docs/accounts-and-users/… -- 参数中提到了光标,developer.twitter.com/en/docs/tweets/timelines/api-reference/… -- 没有光标。 -
@alx 感谢您的回复。那么在这种情况下我应该怎么做分页呢。
标签: php rest twitter twitter-oauth