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';
}
?>