Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-12-2009, 09:02 PM Simple Twitter PHP
Junior Talker

Posts: 3
Trades: 0
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.

Code:
/**
* 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!
CWitt is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-13-2009, 08:22 AM Re: Simple Twitter PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Viral Tweet Generator
Sounds like something budgerigars could catch

Logic says that setting these values "$uName:$pWord" as constants rather than parameters would have the desired effect. So Yes you are correct.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-13-2009, 08:27 AM Re: Simple Twitter PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
or did you want to know how rather than what?

in which case

delete them from the sendTwitter() function declaration and set the values inside the class or in the function
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Simple Twitter PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12591 seconds with 12 queries