#StackBounty: #php #rest #twitter #twitter-oauth PHP – Twitter API (OAuth) with pagination not working properly

Bounty: 50

I have integrated Twitter API (Twitter OAuth) to get latest feeds of particular company account and here below is my code what I have done so far (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) { ?>
                                    
REGENCY CORPORATE

created_at; echo date("F d Y, H:i A", strtotime($date)); ?>

<?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)) { ?>
entities->media[0]->media_url; ?>" alt="Images" height="20%" width="20%" />
<?php } ?> <hr /> </div> <?php } ?>

This works well, I am getting 3 latest tweets. Now I want to add pagination into this, hence I followed documentation provided by Twitter (https://developer.twitter.com/en/docs/basics/cursoring.html), and below is my updated code with cursor for the same and I printed the array (response).

<?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;
?>

As you can see, here I have added $cursor = -1; and updated my api target url to $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser . "&count=" . $notweets . "&cursor=" . $cursor);, passing cursor value.

Here I am getting the 3 recent tweets, however, as per mentioned in documentation from above link (https://developer.twitter.com/en/docs/basics/cursoring.html), you should get response like below.

{
    "ids": [
        385752029,
        602890434,
        ...
        333181469,
        333165023
    ],
    "next_cursor": 1374004777531007833,
    "next_cursor_str": "1374004777531007833",
    "previous_cursor": 0,
    "previous_cursor_str": "0"
}

I have also tried to update requested feed url to this.

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

But I am not getting any keys like next_cursor in any ways so far to be able to proceed. Can someone guide me, what I am doing wrong here, and what should I do to enable pagination from here on?

Any help or suggestion will be highly appreciated.

Thanks


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.