Page 1 of 1

PHP to Twitter Refresh

Posted: Fri May 30, 2008 6:21 pm
by mmsnow04
I'm using a PHP code that updates to Twitter every time it is called. However, Twitter does not take the same message twice in a row, so reloading by itself does not work.

I'm trying to build a way to cycle through messages each time the browser loads the php page. Through tutorials, the method should be by a cookie counter and an array list, however, I'm having trouble.

Here is the working single message code:

Code: Select all

<?php
// Set username and password
$username = 'votingmachine';
$password = 'desmakids';
// The message you want to send
$message = 'voted for B... Tell everyone to come to the Rube Goldberg Voting Machine!';
// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';
// Alternative JSON version
 $url = 'http://twitter.com/statuses/update.json';
 
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
 
// check for success or failure
if (empty($buffer)) {
    echo 'message';
} else {
    echo 'success';
}
 
?>

... And here is the updated code that does not work. When it works, the page displays: success. The Twitter page: http://www.twitter.com/votingmachine/ will be updated.

Code: Select all

<?php
    session_set_cookie_params(60*60*24*7);
    session_start();
    
    if (!is_numeric($_SESSION['counter'])) {
        $_SESSION['counter'] =0;
    }
    
    $_SESSION['counter']++;
 
// Set username and password
$username = 'votingmachine';
$password = 'desmakids';
 
 
// The message you want to send
$message = array('voted for A... tell my friends to vote!...','dude, I voted for A!');
 
 
// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';
// Alternative JSON version
 $url = 'http://twitter.com/statuses/update.json';
 
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
 
 
if ($_SESSION['counter']%2==0) {
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message[0]");
} else {
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message[1]");
}
 
 
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
 
// check for success or failure
if (empty($buffer)) {
    echo 'message';
} else {
    echo 'success';
}
 
 
?>
Any help would be greatly appreciated.

Re: PHP to Twitter Refresh

Posted: Sat May 31, 2008 1:45 pm
by Mr.RED
What you could do is just generate a random number and then match that up to one of the strings in the array and submit that to Twitter.

Now, with only 2 values the chances of getting the same number again are high, but if you have maybe 10 or so different srtings there is less chance of the same one being chosen twice in a row.