Search code examples
phpjqueryajaxtwitter

Creating a reply using PHP and the Twitter API


This is the PHP code im using in conjunction with jQuery .ajax to create a new Tweets:

/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

    $qtweet = $_POST['tweet'];
    $connection->post('statuses/update', array('status' => $qtweet));

Data is supplied by form and a bit of jQuery .ajax:

<form id="tweethis" method='post' action=''>
    <textarea style="width: 42em;" name="tweet" rows="5" id="tweet" ></textarea>
    <br />
    <p class="confirm" style="padding-bottom: 4px;">Tweet Sent.</p>
    <span id="charLeft">140</span>  Characters left
    <br />
    <input type='submit' value='Tweet This!' name='submit' id='submit' />
</form>

$("#tweethis #submit").click(function() {  
    //The Tweet
    var tweettxt = $("textarea#tweet").val();
    var ptweettxt = 'tweet=' + tweettxt;
    $.ajax({
      type: "POST",
      url: "tweet.php",
      data: ptweettxt,
      success: function() {
        $('#tweethis .confirm').css('color','red').slideDown(500).delay(3000).slideUp(500);
        $('textarea#tweet').val("");
      }
    });
    return false;
}); 

I'd like to adapt the form to also deal with replies, so the user can click the ID of a tweet, that ID gets added to the FORM. The FORM will need another hidden field which will be initially blank so when the user submits without a reply ID they just send a Tweet.

However I'll need some code in the PHP file to check if the _POST is empty or null, if null just send a tweet but if it has an ID create a reply.

How can I do the check if the field is empty or filled with an ID?

I also need help creating the reply.

The documentation is here: http://dev.twitter.com/doc/post/statuses/update

This is what I have written so far for the PHP:

$qtweet = $_POST['tweet'];
$tweetid = $_POST['tweetid'];
$connection->post('statuses/update', array('status' => $qtweet, 'in_reply_to_status_id => $tweetid'));

Not sure if I'm contructing the array correctly or this is acceptable to the Twitter API.

The .ajax will be trivial compared to all this as i'll just pass the hidden form value as another value in the _POST.

Any help with this greatly appreciated, this is the last thing left for me to complete my first PHP project, I've created my own Twitter Client specifically designed for use at conferences where a hashtag is used to track the back channel.


Solution

  • you use empty() to check if something is empty =)

    also you need to correct this line...

    $connection->post('statuses/update', array('status' => $qtweet, 'in_reply_to_status_id' => $tweetid));
    

    Don't put the variable in quotes

    Maybe something like

    if ( empty( $_POST['tweetid'] ) ) {
        $connection->post('statuses/update', array('status' => $qtweet));
    }
    else {
        $connection->post('statuses/update', array('status' => $qtweet, 'in_reply_to_status_id' => $_POST['tweetid']));
    }
    

    You just have to put the id of tweet in the form