Im trying to use this Auto tweet generator .php but it wont work


#1

Im using this simple PHP script from 3apps.info using mySQL and php and cron tasks

i cant seem to get it to work and in the tutorial videos i see they are using the old twitter maybe something has changed?

here is the cron file:

<?php

include "config.php";

if($username != '' && $password != '')
{

	$sql = mysql_query("SELECT * FROM messages WHERE (date='".date('Y-m-d')."' OR everyday=1) AND hour='".date('G')."' AND minute='".date('i')."'");

	while($each = mysql_fetch_array($sql)) {

		$result = Twitter::sendTwitter(stripslashes($username),stripslashes($password),$each['message']);
		
		if($each['date']) mysql_query("DELETE FROM messages WHERE id='".$each['id']."'");

	}


}

mysql_query("DELETE FROM messages WHERE date!='' AND date<'".date('Y-m-d')."'");



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($uName='',$pWord='',$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, "$uName:$pWord");
//Attempt to send
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if(strpos($buffer,'<error>') !== false)
{
return false;
}
else
{
return true;
}
}


}

?>

im not really good at PHP so please tell me as simple as you can how i can have a simple FREE auto tweeter

thanks in advance


#2

You cant use your email and password to log in to twitter using the API anymore.
You have to register an app and then use your consumer keys and oAuth to post a tweet.
Like this…
$t = “tweet text”;
$status_url = ‘https://api.twitter.com/1.1/statuses/update.json’;
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_FORM);
$oauth->setToken($userToken,$userSecret);
$args = array(‘status’=>$t);
$oauth->fetch("$status_url",$args,“POST”);
$json = json_decode($oauth->getLastResponse(),true);

Hope this helps


#3

Hi @brianmoreau would it be possible for you to post the full code? I need this cron code to post to my twitter but just can’t get it work, tried both the “twitteroauth” and “tmhOAuth” php libraries with no success.
Thanks