Php Help getting data from mysql and sending it to twitter
Posted: Wed Nov 11, 2009 1:11 pm
Hello I am relatively new to php; I built this code up from bits over the web. I don't get any errors, but it should display the username password message and send it to twitter ,but it won’t display it. You can see it in action at (http://iova66.cphoster.com/test/display.php)
Please help!!
Please help!!
Code: Select all
<?php
class Twitter
{
function Twitter() {
die('Cannot instantiate this class(Twitter) in: '.__FILE__);
}
/**
* Attempts to contact twitter and post a message
*
* @param string $uname = Twitter User Name
* @param string $pWord = Twitter Password
* @param string $message = The message to post through the communication system
* @param string $apiUrl = Twitter API Url. (Optional - defaulted to standard XML API)
* @return boolean
**/
function sendTwitter($username,$pass,$message='',$apiUrl='http://twitter.com/statuses/update.xml')
{
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$apiUrl");
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:$pass");
//Attempt to send
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if(strpos($buffer,'<error>') !== false)
{
return false;
}
else
{
return true;
}
}
}
//get server time
$thetimeis = getdate(time());
$thehour = $thetimeis['hours'];
$theminute = $thetimeis['minutes'];
//display it
echo "$thehour: $theminute";
//database connections
$myServer = "localhost";
$myUser = "username";
$myPass = "password";
$myDB = "database table";
//connection to the database
$dbhandle = mysql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mysql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT twitter_user, twitter_pass, twitter_mesg, hour ";
$query .= "FROM twitter ";
$query .= "WHERE Hour = 0 AND Min = 0 ";
//execute the SQL query and return records
$result = mysql_query($query);
$numRows = mysql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mysql_fetch_array($result))
{
$username =$row['twitter_user'];
$psw =$row['twitter_pass'];
$message =$row['twitter_mesg'];
echo "$twitter_username, $twitter_psw, $twitter_msg";
$ans = Twitter::sendTwitter($message, $username, $pass);
if($ans)
{
echo "We've posted a tweet about your site entry!";
}
}
?>