LoginSignup
3
2

More than 5 years have passed since last update.

(何も書いてない)自分用メモTwistOAuthを使う

Last updated at Posted at 2014-02-26

TwistOAuthの自分用メモ
TwistOAuthれどめ勝手に日本語化 - Qiitaこんなのも作っています

現状

  1. これajaxと夢のコラボしたらUserStreamできるんじゃね?って思ってjavascriptとjQuery勉強してる このへんを見ている(http://www.hp-stylelink.com/news/2013/12/20131206.php)
  2. 自分用メモなのに他人(Twist中の人)が書いている 詳しくは編集履歴を見てください

とりあえずツイートする

Sampleにあったのでコメントをつけただけまっぴーさんが勝手に書き換えてる

<?php

// TwistOAuth.php, CK, CSを読み込む 
require '../settings_for_tests_and_examples.php';
// $_SESSION を使えるようにする
@session_start();

try {

    // リクエストトークンを取りに行く処理
    if (!isset($_SESSION['to'])) {
        // TwistOAuthオブジェクトを新規生成
        $to = new TwistOAuth(new TwistCredential(CK, CS, '', ''));
        // リクエストトークンを取得させる
        $to->postAuto('oauth/request_token');
        // セッションにTwistOAuthオブジェクトを保存(バージョン1.0から可能に)
        $_SESSION['to'] = $to;
        // Twitterに遷移
        header('Location: ' . $_SESSION['to']->credential->getAuthorizeURL()); 
        exit;
    }

    // アクセストークンを取りに行く処理
    if (!isset($_SESSION['authed'])) {
        try {
            if (isset($_GET['oauth_verifier'])) {
                // TwitterのコールバックURLに付加されているoauth_verifierをセットする
                $_SESSION['to']->credential->setVerifier($_GET['oauth_verifier']);
            }
            // アクセストークンを取得させる
            $_SESSION['to']->postAuto('oauth/access_token');
            // ログイン済みであるとしてセッションに記録する
            $_SESSION['authed'] = true;
            // メッセージをセット
            $messages[] = "Hello, @{$_SESSION['to']->credential->screenName}!!";
        } catch (TwistException $e) {
            // 例外が発生したらいったんTwistOAuthオブジェクトを破棄する
            unset($_SESSION['to']);
            throw $e;
        }
    }

    // ツイート処理
    if (isset($_POST['tweet'])) {
        // ツイートする
        $response = $_SESSION['to']->postAuto('statuses/update', array('status' => $_POST['tweet']));
        // 結果メッセージをセット
        $messages[] = "Tweeted: {$response->text}";
    }

} catch (TwistException $e) {

    // エラーメッセージをセット
    $messages[] = $e->getMessage();

}

// UTF-8のHTML文書であることをブラウザに知らせる
header('Content-Type: text/html; charset=utf-8');

?>
<!DOCTYPE html>
<html>
<head>
  <title>TEST</title>
</head>
<body>
<?php if (!empty($messages)): ?>
  <ul>
<?php foreach ($messages as $msg): ?>
    <li><?php echo $msg; ?></li>
<?php endforeach; ?>
  </ul>
<?php endif; ?>
<?php if (isset($_SESSION['authed'])): ?>
  <p>
    <form method="post" action="">
      <input type="text" name="tweet" value="">
      <input type="submit" value="TWEET">
    </form>
  </p>
<?php endif; ?>
</body>
</html>

次はTLを表示したい

function printTL($TwistOAuth){
  foreach($TwistOAuth->getAuto('statuses/home_timeline') as $status)
    var_dump($status);
}
3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2