TwitterOAuthを実装するにあたり色々記事を見かけて実装しているのですが、
3ファイルでのやり方が多いのですが、1ファイルで完結させることはできないでしょうか?
login.php
collback.php
result.php
のように
login.phpでFacebookでアカウント新規作成のようなボタンが押されたらライブラリのインスタンス生成して、Twitterの承認画面へ遷移。
で、コールバックはcollback.phpでユーザー情報を取得してresult.phpへ遷移して結果表示みたいな流れは理解できているのですが、
これを1ファイルで完結させたいと思いできるところまでやってみたのが下記です。
login.php(これはアクセスした時点で承認画面へ飛びます)
<?php session_start(); define( 'CONSUMER_KEY', 'Dqncrr4Oexxxxxxxxxx' ); define( 'CONSUMER_SECRET', '5gJ74RGxxxxxxxxxxxxxxxxxxxxxxxxx' ); define( 'OAUTH_CALLBACK', 'http://dev.xxxxx.com/callback.php' ); require_once(__DIR__ . '/lib/Loader.php'); //TwitterOAuth をインスタンス化 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); //コールバックURLをここでセット $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); //callback.phpで使うのでセッションに入れる $_SESSION['oauth_token'] = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; //Twitter.com 上の認証画面のURLを取得( この行についてはコメント欄も参照 ) $url = $connection->url('oauth/authenticate', array('oauth_token' => $request_token['oauth_token'])); //Twitter.com の認証画面へリダイレクト header( 'location: '. $url ); ?>
collback.phpとresult.phpを凝縮。
php
1<?php 2 3session_start(); 4 5define( 'CONSUMER_KEY', 'Dqncrr4Oexxxxxxxxxx' ); 6define( 'CONSUMER_SECRET', '5gJ74RGxxxxxxxxxxxxxxxxxxxxxxxxx' ); 7define( 'OAUTH_CALLBACK', 'http://dev.xxxxx.com/callback.php' ); 8 9require_once(__DIR__ . '/lib/Loader.php'); 10 11//OAuth トークンも用いて TwitterOAuth をインスタンス化 12$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 13 14//アプリでは、access_token(配列になっています)をうまく使って、Twitter上のアカウントを操作していきます 15$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_GET['oauth_verifier'])); 16 17//OAuthトークンとシークレットも使って TwitterOAuth をインスタンス化 18$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 19 20//ユーザー情報をGET 21$user = $connection->get("account/verify_credentials", ['include_entities'=> 'false', 'include_email'=> 'true']); 22 23//GETしたユーザー情報をvar_dump 24echo "<pre>"; 25var_dump( $user ); 26echo "</pre>"; 27 28?>
話は少し変わりますが
Login with Facebookではとりあえずできました。
なのでTwitterも実装しようと思ったのですが、少しつまづいてます。
下記は作りかけですがみて頂ければわかると思いますが、
Facebookは”ログインできてたら”という判定で処理しているので
それと同じようにTwitterもやろうと思ったのですが、どうすれば良いのかわかりません。
<?php //autoloadによる全ファイル自動読込。 require_once(__DIR__ . '/lib/Loader.php'); require_once(__DIR__ . '/lib/config/set_config.php'); if(!empty($_POST['submit'])){ $obj = new CreateAccount($cfg_set); //入力値取得 $mail = filter_input(INPUT_POST, 'mail'); $password = filter_input(INPUT_POST, 'password'); $password_confir = filter_input(INPUT_POST, 'password_confir'); //全項目入力しているか確認 $checkTheInput = $obj->checkTheInput($mail, $password, $password_confir); if($checkTheInput == true) { //入力したアドレスがDBに存在しないか確認 $existenceCheckOfMail = $obj->existenceCheckOfMail($mail); if($existenceCheckOfMail == true) { //入力したパスワードが一致しているか確認 $passwordMatch = $obj->passwordMatch($password, $password_confir); } } //仮登録処理 if(isset($passwordMatch)) { if($passwordMatch == true) { //account_manage_dumpテーブルへ仮登録 $tmpRegistration = $obj->tmpRegistration($mail, $password); //仮登録のtokenをURLに付与してメール発行。 mb_language("Japanese"); mb_internal_encoding("UTF-8"); $to = $mail; $subject = "【GAMES】仮登録完了のお知らせ"; $message = "GAMESへご登録頂きましてありがとうございます。"."\r\n"; $message .= "下記URLにアクセス後、本登録完了となります。"."\r\n"; $message .= "http://localhost/dev/personal/web/game_package/create_account_finish.php?token=".$tmpRegistration. "\r\n"; $message .= "※URL有効期限 : ".$obj->now('+ 1 day'). "\r\n"; $message .= "登録した覚えがない場合はこのメールを破棄してください。"."\r\n"; $headers = 'From: info@test.co.jp' . "\r\n"; mb_send_mail($to, $subject, $message, $headers); } } //デバッグ $obj->var_dumpre($obj->msg); } else if(!empty($_POST['submit_fb'])) { var_dump($_POST['submit_fb']."が押されました。"); $config = array( 'appId' => '1199599606787650', 'secret' => 'fdfb8ecc512196df9ea1b83ba8d65a38' ); $facebook = new Facebook($config); //ログイン済みの場合はユーザー情報を取得 if ($facebook->getUser()) { try { /* 汎用性のあるもの fields=locale,id,email,last_name,first_name,middle_name,name,gender */ $user = $facebook->api( '/me?fields=email,last_name,first_name' ,'GET'); //birthday,languages, } catch(FacebookApiException $e) { //取得に失敗したら例外をキャッチしてエラーログに出力 error_log($e->getType()); error_log($e->getMessage()); } } if (isset($user)) { //ログイン済みでユーザー情報が取れていれば表示 echo '<pre>'; var_dump($user); echo '</pre>'; } else { //未ログインならログイン URL へ遷移。 header( "Location: " .$facebook->getLoginUrl() ); exit; } } else if(!empty($_POST['submit_tw'])) { var_dump($_POST['submit_tw']."が押されました。"); define( 'CONSUMER_KEY', 'Dqncrr4Oexxxxxxxxxx' ); define( 'CONSUMER_SECRET', '5gJ74RGxxxxxxxxxxxxxxxxxxxxxxxxx' ); define( 'OAUTH_CALLBACK', 'http://dev.xxxxx.com/callback.php' ); //TwitterOAuth をインスタンス化 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); //コールバックURLをここでセット $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK)); var_dump($_GET['oauth_verifier']); /* $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']); if() { //ユーザー情報をGET $user = $connection->get("account/verify_credentials", ['include_entities'=> 'false', 'include_email'=> 'true']); } */ if (isset($user)) { //ログイン済みでユーザー情報が取れていれば表示 echo '<pre>'; var_dump($user); echo '</pre>'; } else { //未ログインならログイン URL へ遷移。 $url = $connection->url('oauth/authenticate', array('oauth_token' => $request_token['oauth_token'])); header( 'location: '. $url ); exit; } } var_dump($_POST); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="ja"> <meta charset="UTF-8"> <meta name="robots" content="index,follow"> <meta name="description" content=""> <meta name="keywords" content=""> <meta name="author" content=""> <title>アカウント作成</title> <link rel="stylesheet" href="" /> <script type="text/javascript" src=""></script> </head> <header class=""> </header> <body> <header class=""> </header> <article> <form action="" method="post" accept-charset="utf-8"> <table border=""> <caption>アカウント作成</caption> <tbody> <tr> <th>email</th> <td><input type="email" name="mail" maxlength="50" value= <?= filter_input(INPUT_POST, 'mail'); ?> ></td> </tr> <tr> <th>password</th> <td><input type="password" name="password" minlength="6" maxlength="50" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="半角数字 小文字の半角アルファベット 大文字の半角アルファベット これらが組み合わさった6文字以上。"></td> </tr> <tr> <th>password(再入力)</th> <td><input type="password" name="password_confir" minlength="6" maxlength="50" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="半角数字 小文字の半角アルファベット 大文字の半角アルファベット これらが組み合わさった6文字以上。"></td> </tr> </tbody> </table> <input type="submit" name="submit" value="登録"> <input type="submit" name="submit_fb" value="Facebookで作成"> <input type="submit" name="submit_tw" value="Twitterで作成"> </form> </article> </body> <footer> <nav> </nav> </footer> </html>
SESSION使って回って取得するしかないですかね?
追記
http://qiita.com/sofpyon/items/982fe3a9ccebd8702867
http://noumenon-th.net/programming/2016/03/08/twitteroauth/
回答1件
あなたの回答
tips
プレビュー