views:

161

answers:

1

Im using the Twitter API to collect the number of tweets I've favorited, well to be accurate the total pages of favorited tweets.

I use this URL: http://api.twitter.com/1/users/show/username.xml

I grab the XML element 'favorites_count'

For this example lets assume favorites_count=5

The Twitter API uses this URL to get the favorties: http://twitter.com/favorites.xml (Must be authenticated)

You can only get the last 20 favorties using this URL, however you can alter the URL to include a 'page' option by adding: ?page=3 to the end of the favorites URL e.g.

http://twitter.com/favorites.xml?page=2

So what I need to do is use CURL (I think) to collect the favorite tweets, but using the URL:

http://twitter.com/favorites.xml?page=1

http://twitter.com/favorites.xml?page=2

http://twitter.com/favorites.xml?page=3

http://twitter.com/favorites.xml?page=4

etc...

Some kind of loop to visit each URL, and collect the Tweets and then output the cotents.

Can anyone help with this: - Need to use CURL to authenticate - Collect the number of pages of tweets (Already scripted this) - Then use a loop to go through each page URL based on the pages value?

A: 

Is favorites_count the total number of favorites or the total number of pages?

$twitter = curl_init('http://api.twitter.com/1/users/show/username.xml');
curl_set_opt($twitter, CUROPT_RETURNTRANSFER, true);

$userInfo = curl_exec($twitter);
$userObj = new SimpleXmlElement($userInfo);
$nbFaves = $userObj->favorites_count; // (string) 5

$urlTpl = "http://http://twitter.com/favorites.xml?page=%s";
$favorites = array(); // this will be the data for output

for($i =0; $i < $nbFaves; $i++) {
  $pageUrl = sprintf($urlTpl, $i+1); // notice the +1 to move from 0 indexed to 1 indexed
  curl_set_opt($twitter, CURLOPT_URL, $pageUrl);
  $faves = curl_exec($twitter);
  $faves = new SimpleXmlElement($faves);

  foreach($faves->favorite as $fave) {
     $data = array();
     /* here you assign the diferent child values/attributes 
      * from the favorite node to the $data array
      */
     $favorites[] = $data; // push the data array into $favorites
  }

  unset($faves); // just some cleanup

}

// now you would loop through $favorites and output the data as html.

Now if favorites_count is the total number of favorites and not pages then you need to modify the above to figure out how many pages there are based on how many favorites are on a page. but thats pretty simple.

prodigitalson
favorites_count returns the number of favourites, but I divide that by 20 and the round the number up to get the total number of pages.
danit
Tried it get call to undefined function.
danit
on what line/function call?
prodigitalson