tags:

views:

227

answers:

0

I'm trying to fetch all the 'favorited' tweets made by myself.

Firstly the API only returns the last 20 favorited tweets via this URL:

http://twitter.com/favorites.xml (Must be authenticated to view)

I can use a URL like this, to fetch a different page:

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

Im using this code to work out the number of Favorites the user has:

//Twitter Details
$username = "bob";
$password = "password";

//Get number of favorites
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name='.$username.'');
if (preg_match('/favourites_count>(.*)</',$xml,$match)!=0) {
    $tw['count'] = $match[1];
}
$favs = $tw['count'];

//Twitter API returns max 20 favorites per page
$maxperpage = 20;
//Divide the total number of favorites by max per page
$totalpages = ceil($favs / $maxperpage);
//Total rounded up so we get final page
echo $totalpages;

Dividing the total number of favorites by 20 then rounding that number up gives the total number of pages I need to loop through.

I'm then using this code to get the latest 20 tweets:

$twitterHost = "http://twitter.com/favorites.xml";
$curl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_URL, $twitterHost);
$result = curl_exec($curl);
curl_close($curl);

echo $result;

I now need to combine the above 2 scripts to loop through the script the right amount of times to get all favorites...

Can anyone help?