views:

135

answers:

4

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.

A: 

You could have the hidden field on the form all the time with a default value of 0. Then in your click closure use obtain the value of the hidden field and submit it in your ajax post.

The PHP controller can then look at the posted value, if it is 0 it should build the post to twitters api as a status update. When it is greater than 0 build the post to include the 'in_reply_to_status' property with the value obtained from the form post.

Then your "reply" link will have some JS to update that hidden form element with the proper ID.

Not sure about how to construct the post to twitters api itself though. Hope this helps some.

Flash84x
A: 

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

Galen
While the code is correct, it doesnt seem to generate a reply. The in_reply_to... part doesnt get populated when the tweet is sent.
danit
I fixed an error in the code. You have to make sure $_POST['tweetid'] is set
Galen
A: 

I think the problem is in this snippet

'in_reply_to_status_id => $tweetid'

It should be

'in_reply_to_status_id' => $tweetid

Other than that, Galen's answer is worth consideration.

silent
A: 

Here is some PHP that will include in_reply_to_status_id if $_POST['tweetid'] is not empty.

<?php
$options = array();
$options['status'] = $_POST['tweet'];
if (!empty($_POST['tweetid']) {
  $options['in_reply_to_status_id'] = $_POST['tweetid'];
}
$connection->post('statuses/update', $options);

One final issue with the Twitter API is that replies must contain the screen_name of the user being replied to. So if @bob is replying to @sarah then @sarah must be contained within $_POST['tweet']. This screen_name should be included in the textbox the user is typing the reply in.

abraham