views:

770

answers:

5

How can i get All my followers with one request? If i do $this->twitter->getFollowers(); I just get 100 of my followers;

An here is the code for getting followers

/**
     * Returns the authenticating user's followers.
     *
     * @return array
     * @param string[optional] $id  The id or screen name of the user for whom to request a list of followers.
     * @param int[optional] $page
     */
    public function getFollowers($id = null, $page = null)
    {
     // build parameters
     $aParameters = array();
     if($page !== null) $aParameters['page'] = (int) $page;

     // build url
     $url = 'statuses/followers.xml';
     if($id !== null) $url = 'statuses/followers/'. urlencode($id) .'.xml';

     // do the call
     $response = $this->doCall($url, $aParameters, true, false);

     // convert into xml-object
     $xml = @simplexml_load_string($response);

     // validate
     if($xml == false) throw new TwitterException('invalid body');

     // init var
     $aUsers = array();

     // loop statuses
     foreach ($xml->user as $user) $aUsers[] = $this->userXMLToArray($user);

     // return
     return (array) $aUsers;
    }
A: 

If I see that right, you're accessing the URL statuses/followers/[...].xml. It seems that this is restricted to 100 users per page, to get additional pages and so more users, use the URLs statuses/followers/[...].xml?page=N with N >= 2.

As The MYYN and others stated, this isn't correct use anymore, and you should use statuses/followers/[...].xml?cursor=-1 and similar.

schnaader
from the twitter api docs: The previously documented page-based pagination mechanism is still in production, but will be deprecated. Please migrate to cursor-based pagination for increased reliability and performance.
The MYYN
Ah, thanks for the clarification.
schnaader
+5  A: 

When you call $this->twitter->getFollowers(); you basically hitting the 100 followers per page limit. Try to make use of the paging to subsequentially access all your followers, if you happen to have more than 100 ...

See also: http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0followers

cursor. Optional. Breaks the results into pages. A single page contains 100 users. This is recommended for users who are followed by many other users. Provide a value of -1 to begin paging. Provide values as returned to in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.

Example: http://twitter.com/statuses/followers/barackobama.xml?cursor=-1

Example: http://twitter.com/statuses/followers/barackobama.xml?cursor=1300794057949944903

The MYYN
+1  A: 

You should be using the twitter API so you can call http://twitter.com/followers/ids.format That allows you to page the results using the cursor parameter. See the page here

Jeff Beck
+2  A: 

As others have mentioned, there's a limit of 100 users returned on that API call, and yes, you should be switching from page-based to cursor-based calls Real Soon Now.

To actually answer your question, though...check and see if the number of results returned is == 100, and if so, call $this->twitter->getFollowers(null, $page); where $page is a counter that you increment. If you get back < 100 results, that's the last page.

Michael D. Ivey
Checking to see if the number of results is 100 won't always work, the result set isn't guaranteed to be 100 every time as suspended users will be filtered out. http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses%C2%A0followers
wf
Good point. I need to go tweak a script (or just go ahead and update it to cursors). I guess you could keep checking to see if the result set is empty, which should mean you've hit the last page.
Michael D. Ivey
+1  A: 

Get all followers change API http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses%C2%A0followers

public function getMyContacts() {

    if (file_exists($this->getLogoutPath())) 
        {$auth=explode("/",file_get_contents($this->getLogoutPath()));$user=$auth[0];$pass=$auth[1];}
    else return false;
    $res=$this->get("http://{$user}:{$pass}@twitter.com/statuses/followers.xml",true);

    $cursorNext = array();
    $cursorNext[0] = -1;
    do {
    if($cursorNext[0] == -1 || $cursorNext[0] !=0)
    {
        $res = $this->get("http://{$user}:{$pass}@twitter.com/statuses/followers.xml?cursor={$cursorNext[0]}",true);

    } else {
        break;
    }

    if ($this->checkResponse('responce_ok_followers',$res))
        $this->updateDebugBuffer('responce_ok_followers',"http://user:[email protected]/statuses/followers.xml?cursor={$cursorNext[0]}",'GET');
    else 
        {
        $this->updateDebugBuffer('responce_ok_followers',"http://user:[email protected]/statuses/followers.xml?cursor={$cursorNext[0]}",'GET',false);
        $this->debugRequest();
        $this->stopPlugin();
        return false;   
        }

        $tempres .= $res;
        $cursorNext = $this->getElementDOM($res,'//users_list/next_cursor');

    }while(1);

    $contacts = $this->getElementDOM($tempres,'//screen_name');
    return $contacts;   
    }

By FRAG =)

FRAG
Thanks for tthe code
streetparade
can you please tell me where you got this code?
streetparade