Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 896
CLiown Avatar asked Nov 15 '22 07:11

CLiown


1 Answers

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.

like image 179
Chris Wagner Avatar answered Nov 17 '22 05:11

Chris Wagner