PHP – Twitter API(OAuth),分页无法正常工作

前端之家收集整理的这篇文章主要介绍了PHP – Twitter API(OAuth),分页无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经集成了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,$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)提供的文档,下面是我更新的代码,光标相同,我打印了数组(响应).

<?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,$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目标网址更新为$tweets = $connection-> get(“https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”.$twitteruser.“& count =”. $notweets.“& 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"
}

我还尝试更新请求的Feed网址.

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser .  "&cursor=" . $cursor);

但到目前为止,我无法以任何方式得到任何像next_cursor这样的键才能继续.有人可以指导我,我在这里做错了什么,我该怎样做才能从这里开始分页

任何帮助或建议将受到高度赞赏.

谢谢

Refer Link :: Github :: https://github.com/dineshghule321/codebird-php

使用Codebird从您的PHP代码连接到Twitter REST API,Streaming API,Collections API,TON(Object Nest)API和Twitter Ads API – 所有这些都只使用一个库. Codebird支持完整的3路OAuth以及仅限应用程序的身份验证.

原文链接:https://www.f2er.com/php/136947.html

猜你在找的PHP相关文章