PHP - Twitter API (OAuth) with pagination not working properly

test twitter api
twitter api request example
twitter api authentication example
how to use twitter api
twitter api example
twitter free api
twitter api response
twitter ads api example

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) { ?>
                                    <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 
                            } ?>

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

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

Use Codebird to connect to the Twitter REST API, Streaming API, Collections API, TON (Object Nest) API and Twitter Ads API from your PHP code — all using just one library. Codebird supports full 3-way OAuth as well as application-only auth.

php, Browse other questions tagged php twitter-oauth or ask your own question. PHP - Twitter API (OAuth) with pagination not working properly  Home » Php » PHP – Twitter API (OAuth) with pagination not working properly PHP – Twitter API (OAuth) with pagination not working properly Posted by: admin October 20, 2018 Leave a comment

The standard paging approaches are not suitable for methods like GET statuses/user_timeline due to the Twitter's realtime nature.

https://developer.twitter.com/en/docs/tweets/timelines/guides/working-with-timelines

The cursoring as you have requested only applies if you need to retrieve a very large data instead of retrieving all at once for some methods such as GET friends/ids in which you can send the cursor as a parameter. In other words the method needs to support the cursor as a parameter.

https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-ids

Tips and tricks for API pagination, Sometimes you need more from your API than what you get from the first set of returned results. API than what you get from the first set of returned results. Twitter posts at once, the big list of posts is broken up into different “pages”. /​square/connect-php-sdk/blob/master/lib/ApiClient.php#L288)in PHP. The Wid'op OAuth library is a modern PHP 5.3+ API allowing you to easily obtain a Twitter access token. For now, it supports OAuth Web & Application tokens (not xOAuth). Here a sample for the Web workflow:

OK Guys,

I have found my own way to solve this to integrate ajax pagination using my same library which I have already used. Here is the way what I have done.

I have created two pages. One to send ajax request to grab data in results (page1.php), second page is to fetch api and based on ajax request (twitter_posts_ajax.php).

page1.php

<div class="col-xs-12 twitter_posts">
</div>

<script>

$( document ).ready(function() {

      $.ajax({
            type: 'POST',
            url: "twitter_posts_ajax.php",      
            dataType: "html",
                success: function(resultData) { 
                console.log(resultData);
                    $( ".twitter_posts" ).html(resultData);
                }
            });


             $(document).on('click', '.twitter_posts_anchor', function(){

        var twitterpagenumber = $(this).data('twitterpagenumber');
        var myKeyVals = { twitterInpage : twitterpagenumber}

        $.ajax({
        type: 'POST',
        url: "twitter_posts_ajax.php",
        data: myKeyVals,
        dataType: "html",
            success: function(resultData) { 
                console.log(resultData);
                $( ".twitter_posts" ).html(resultData);

                $('html, body').animate({
                        scrollTop: $( '.twitter_posts' ).offset().top
                    }, 500);

            }
        });

    });
});

twitter_posts_ajax.php

<?php 
session_start();
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library

$twitteruser = "CompanyName";
$limit = 10; //per page    


$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);

if (!isset($_SESSION['twitter_total_pages'])) {
    // Counting all tweets first 
    $count_all_tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser);
    $twitter_total_pages = ceil(count($count_all_tweets) / $limit);
    $_SESSION['twitter_total_pages'] = $twitter_total_pages;
} else {
    $twitter_total_pages = $_SESSION['twitter_total_pages'];
}





$twitterInpage = !empty($_POST['twitterInpage']) ? (int)$_POST['twitterInpage'] : 1;


$totalPages = $_SESSION['twitter_total_pages']; //calculate total pages
$twitterInpage = max($twitterInpage, 1); //get 1 page when $_GET['page'] <= 0
$twitterInpage = min($twitterInpage, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($twitterInpage - 1) * $limit;

if ($offset < 0) $offset = 0;



$link = '?twitterInpage=%d';
$pagerContainer = '<div style="width: 300px;" class="linkedin_pagination">';
if ($totalPages != 0) {
    if ($twitterInpage == 1) {
        $pagerContainer .= '';
    } else {


        $page_number = $twitterInpage - 1;
        $pagerContainer .= sprintf('<a href="javascript:void(0)" style="color: #c00" class="twitter_posts_anchor" data-twitterpagenumber=' . $page_number . '> &#171; prev page</a>');
    }
    $pagerContainer .= ' <span> page <strong>' . $twitterInpage . '</strong> from ' . $totalPages . '</span>';
    if ($twitterInpage == $totalPages) {
        $pagerContainer .= '';
    } else {
        $page_number = $twitterInpage + 1;
        $pagerContainer .= sprintf('<a href="javascript:void(0)" style="color: #c00" class="twitter_posts_anchor" data-twitterpagenumber=' . $page_number . '> next page &#187; </a>');

    }
}
$pagerContainer .= '</div>';



if ($twitterInpage == $_SESSION['twitter_total_pages']) {
    $twitter_page_number = $_SESSION['twitter_total_pages'];
} else {
    $twitter_page_number = ($page_number - 1);
}

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


                                <?php foreach ($tweets as $current_tweet) { ?>
                                    <div class="card">
                                        <div class="card-body">
                                            <div class="media">
                                                <div class="newsIcon">
                                                    <img src="img/theme-logo-icon.png" alt="logo" />
                                                </div>
                                                <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" />
                                        </div>
                                        <?php 
                                    } ?>

                                    </div>
                                <?php 
                            } ?>
<?php echo $pagerContainer; ?>

Here, as you can see, we have two pages, page1.php through which I am passing ajax request to load data from twitter_posts_ajax.php.

I have also used session_start() in twitter_posts_ajax.php to grab total number of tweets only once so that I do not need to recount every time. I have also used offset and limit for my pagination which works well with ajax.

Hope this helps.

Making Authenticated Requests, Before making authenticated requests to the Twitter Ads API, you will need: You will need to add support to generate OAuth 1.0a Authorization request For those who are not familiar with a command-line tool, we also provide a both the application's and user's identity as well as prevents tampering with the request. If you are already familiar with the OAuth protocol or you do not care much about its reason of existence, you may want to skip to the section below named Using the PHP OAuth API class in practice. Consider one user that is registered to one site, lets call it the provider or server site .Think for instance of Twitter as an example of a provider site.

Pagination, The paging mechanism is easy to use and very similar to the REST API's cursor-​based Note: the analytics endpoints do not support this style of pagination. The simplest PHP Wrapper for Twitter API v1.1 calls - J7mbo/twitter-api-php. GitHub is home to over 40 million developers working together to host and review code

Accessing Ads accounts, Error codes and responses · Pagination · Sorting · Mobile App Promotion There are two methods you can use to make Ads API requests for an Applications that do not have a publicly accessible UI exposed to advertisers can Access tokens obtained through the 3-legged OAuth flow for your account will be able to  The Twitter API only provides access to up to 100 Retweeters (IDs for users that RT'd a Tweet), regardless of whether this is your owned Tweet, or another one. As for a specific app, it is only possible to speculate, unless the source code was Open Source and available.

Using the Graph API, More detail about the various operations you can perform with the Graph API. directive on facebook.com , but this should not adversely affect your Graph API calls. OAuth 2.0 allows entities such as a User or a Page to authorize tokens. make working with access tokens much simpler than building your own solution. abraham / twitteroauth. Code Issues 35 Pull requests 13 Actions Security Insights. Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. The most popular PHP library for use with the Twitter OAuth REST API. https://twitteroauth.com.

Comments