tripper_john_md
Newbie
- Feb 21, 2011
- 40
- 35
They need to authorize your app, during this process you should get the keys.
//CHECK THE TWEET ID OF THE MOST RECENTLY SENT TWEET
$logfile = 'last_send_tweetID.txt';
$f = fopen($logfile, 'r');
$lasttweetID = fgets($f);
fclose($f);
//GET MOST RECENT TWEET FROM DONOR ACCOUNT, EXCLUDE RETWEETS AND REPLIES
$notweets = 1;
$ignore_replies = true; // Ignore replies from the timeline. (Default : false)
$include_rts = false; // Include retweets. (Default : false)
$twitteruser = "DONOR ACCOUNT HERE";
//SET USER KEYS (based on tmeyertweets twitter account)
$consumerkey = "xxxxxxxxxxxxxxx";
$consumersecret = "xxxxxxxxxxxxxxxxxxxx";
$accesstoken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$accesstokensecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
// DECLARE FUNCTION
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;
}
// MAKE CONNECTION, GET MOST RECENT TWEET
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets."&include_rts=".$include_rts."&exclude_replies=".$ignore_replies);
$tweetData = json_decode(json_encode($tweets), true);
//CHECK TO SEE IF MOST RECENT TWEET IS NEWER THAN MOST RECENTLY SENT RETWEET
if ($lasttweetID<$tweetData[0]["id_str"])
{
//SEND THE RETWEET
// Create object
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
// POST REWEET
$retweetResult = $tweet->post('statuses/retweet/'.$tweetData[0]["id_str"]);
// UPDATE THE LASTTWEETEDID file
$f = fopen($logfile, 'w');
fwrite($f, $tweetData[0]["id_str"]);;
fclose($f);
}
else
{
// LOG THAT NO TWEET WAS DONE
$logoutput = "There is NO new tweet available.\r\n";
$file2 = 'retweet_log.txt';
file_put_contents($file2, $logoutput, FILE_APPEND | LOCK_EX);
}
// Create object
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$postID = "382904915707691008";
// Post the retweet
$retweetResult = $tweet->post('statuses/retweet/'.$postID);
$favoriteResult = $tweet->post('favorites/create/'.$postID);
var_dump($retweetResult);
echo "</p>";
var_dump($favoriteResult);
object(stdClass)#18 (1) { ["errors"]=> array(1) { [0]=> object(stdClass)#19 (2) { ["message"]=> string(31) "Sorry, that page does not exist" ["code"]=> int(34) } } }
<?php
$tweet->post('favorites/create',array('id'=>$postID));
?>
$row = 1;
if (($handle = fopen("retweets.csv", "r")) !== FALSE) {
while (($userdata = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($userdata);
$row++;
// Create object for each user where $userdata[3] and [4] are access keys specific to each user
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $userdata[3], $userdata[4]);
$postID = "382485048274202624";
// Post the retweet
$retweetResult = $tweet->post('statuses/retweet/'.$postID);
}
}