質問編集履歴

1

callback.php追記

2018/04/30 07:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -70,12 +70,86 @@
70
70
 
71
71
 
72
72
 
73
-
74
-
75
-
76
-
77
73
  ```ここに言語を入力
78
74
 
75
+ 【callback.php】
76
+
77
+ <?php
78
+
79
+
80
+
81
+ session_start();
82
+
83
+
84
+
85
+ require_once 'common.php';
86
+
87
+ require_once 'twitteroauth/autoload.php';
88
+
89
+
90
+
91
+ use Abraham\TwitterOAuth\TwitterOAuth;
92
+
93
+
94
+
95
+ //login.phpでセットしたセッション
96
+
97
+ $request_token = []; // [] は array() の短縮記法。詳しくは以下の「追々記」参照
98
+
99
+ $request_token['oauth_token'] = $_SESSION['oauth_token'];
100
+
101
+ $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
102
+
103
+
104
+
105
+ //Twitterから返されたOAuthトークンと、あらかじめlogin.phpで入れておいたセッション上のものと一致するかをチェック
106
+
107
+ if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) {
108
+
109
+ die( 'Error!' );
110
+
111
+ }
112
+
113
+
114
+
115
+ //OAuth トークンも用いて TwitterOAuth をインスタンス化
116
+
117
+ $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
118
+
119
+
120
+
121
+ //アプリでは、access_token(配列になっています)をうまく使って、Twitter上のアカウントを操作していきます
122
+
123
+ $_SESSION['access_token'] = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
124
+
125
+ /*
126
+
127
+ ちなみに、この変数の中に、OAuthトークンとトークンシークレットが配列となって入っています。
128
+
129
+ */
130
+
131
+
132
+
133
+ //セッションIDをリジェネレート
134
+
135
+ session_regenerate_id();
136
+
137
+
138
+
139
+ //マイページへリダイレクト
140
+
141
+ header( 'location: /mypage.php' );
142
+
143
+
144
+
145
+ ```
146
+
147
+
148
+
149
+
150
+
151
+ ```ここに言語を入力
152
+
79
153
  【logout.php】
80
154
 
81
155