Jump to content

Simple Twitter PHP


CWitt

Recommended Posts

I really could use a hand here. I'm working with this Viral Tweet Generator and I want to alter the code so that the HTML form automatically post directly to Twitter. Right now, the php has the form take the visitor's username and password and then posts the message. I would rather have the message just posted on a certain Twitter account that already has the user/pass in the PHP. I would assume it would be as simple as declaring the variable, but my mind seems feeble as I can't figure out why the script won't work.

 

/**
* CONFIGURATION SETTINGS
* string $message = Message to post on Twitter
* string $callingUrl = File the post originated from (or where to return on failure)
* string $successUrl = URL to go to on completion
**/

$message = "Just reading about this site: it's bonkers!";
$callingUrl = "error.html";
$successUrl = "success.html";

if($_POST['username'] != '' && $_POST['password'] != '')
{
   $result = Twitter::sendTwitter(stripslashes($_POST['username']),stripslashes($_POST['password']),$message);
   if($result)
   {
      header('Location: ' . $successUrl);
      exit;
   }   
}
header('Location: ' . $callingUrl);

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

?>

 

So essentially, I just want to declare $uname & $pWord and I'll just remove that part of the HTML form.

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/148507-simple-twitter-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.