<?php
  // POSTでTweetする文言が渡されてきたときだけPHPな処理をする
  if ( isset( $_POST[ 'tweet_this' ] ) and !empty( $_POST[ 'tweet_this' ] ) )
  {
    // TwitterOAuthのライブラリをインクルード
    require_once( 'twitteroauth.php' );

    // Twitter APIを使うために必要なキーを定義する
    $consumer_key        = '';
    $consumer_secret     = '';
    $access_token        = '';
    $access_token_secret = '';

    // TwitterOAuthのオブジェクトを作る
    $obj_twitter = new TwitterOAuth( $consumer_key
                                   , $consumer_secret
                                   , $access_token
                                   , $access_token_secret );

    // Tweetする
    $ret = $obj_twitter->OAuthRequest( 'https://twitter.com/statuses/update.json'
                                     , 'POST'
                                     , array( 'status'=>$_POST[ 'tweet_this' ] ) );

    // レスポンス（JSON形式）をデコードする
    $decoded_ret = json_decode( $ret, true );

    // エラーだったら渡されてきたエラーメッセージをそのまま表示する
    if ( isset( $decoded_ret[ 'error' ] ) )
    {
      $message = 'Tweet失敗！：' . $decoded_ret[ 'error' ];
    } // end of if
    else
    {
      $message = 'Tweetしました♪';
    } // end of else
  } // end of if
  else
  {
    $message = 'フォームに入力したものをTweetするよ♪';
  } // end of else
?>
<html>
  <header>
    <title>Example01：フォームに入力するとそれをTweetするよ♪</title>
  </header>
  <body>
    <p><?php echo $message; ?></p>
    <form method="post" action="example01.php">
      <textarea name="tweet_this"></textarea>
      <input type="submit" value="Tweet!" />
    </form>
  </body>
</html>
