-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-token.php
More file actions
102 lines (88 loc) · 3.2 KB
/
get-token.php
File metadata and controls
102 lines (88 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* ベアラートークンの取得
* https://syncer.jp/twitter-api-matome
* 2015-09-01
*/
// 設定項目
$api_key = '' ; // APIキー
$api_secret = '' ; // APIシークレット
// クレデンシャルを作成
$credential = base64_encode( $api_key . ':' . $api_secret ) ;
// リクエストURL
$request_url = 'https://api.twitter.com/oauth2/token' ;
// リクエスト用のコンテキストを作成する
$context = array(
'http' => array(
'method' => 'POST' , // リクエストメソッド
'header' => array( // ヘッダー
'Authorization: Basic ' . $credential ,
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' ,
) ,
'content' => http_build_query( // ボディ
array(
'grant_type' => 'client_credentials' ,
)
) ,
) ,
) ;
// cURLを使ってリクエスト
$curl = curl_init() ;
curl_setopt( $curl , CURLOPT_URL , $request_url ) ;
curl_setopt( $curl , CURLOPT_HEADER, 1 ) ;
curl_setopt( $curl , CURLOPT_CUSTOMREQUEST , $context['http']['method'] ) ; // メソッド
curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false ) ; // 証明書の検証を行わない
curl_setopt( $curl , CURLOPT_RETURNTRANSFER , true ) ; // curl_execの結果を文字列で返す
curl_setopt( $curl , CURLOPT_HTTPHEADER , $context['http']['header'] ) ; // ヘッダー
curl_setopt( $curl , CURLOPT_POSTFIELDS , $context['http']['content'] ) ; // リクエストボディ
curl_setopt( $curl , CURLOPT_TIMEOUT , 5 ) ; // タイムアウトの秒数
$res1 = curl_exec( $curl ) ;
$res2 = curl_getinfo( $curl ) ;
curl_close( $curl ) ;
// 取得したデータ
$json = substr( $res1, $res2['header_size'] ) ; // 取得したデータ(JSONなど)
$header = substr( $res1, 0, $res2['header_size'] ) ; // レスポンスヘッダー (検証に利用したい場合にどうぞ)
// [cURL]ではなく、[file_get_contents()]を使うには下記の通りです…
// $response = @file_get_contents( $request_url , false , stream_context_create( $context ) ) ;
// JSONをオブジェクトに変換
$obj = json_decode( $json ) ;
// HTML用
$html = '' ;
// 実行結果を出力
$html .= '<h2>実行結果</h2>' ;
// エラー判定
if( !$obj || !isset( $obj->access_token ) )
{
$html .= '<p>トークンを取得することができませんでした…。設定を見直して下さい。</p>' ;
}
else
{
// 各データ
$bearer_token = $obj->access_token ;
// 出力する
$html .= '<dl>' ;
$html .= '<dt>ベアラートークン</dt>' ;
$html .= '<dd>' . $bearer_token . '</dd>' ;
$html .= '</dl>' ;
}
// 検証用にレスポンスヘッダーを出力 [本番環境では不要]
$html .= '<h2>取得したデータ</h2>' ;
$html .= '<p>下記のデータを取得できました。</p>' ;
$html .= '<h3>ボディ</h3>' ;
$html .= '<p><textarea rows="8">' . $json . '</textarea></p>' ;
$html .= '<h3>レスポンスヘッダー</h3>' ;
$html .= '<p><textarea rows="8">' . $header . '</textarea></p>' ;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ベアラートークンの取得</title>
</head>
<body>
<?php
// ブラウザに[$html]を出力 (HTMLのヘッダーとフッターを付けましょう)
echo $html ;
?>
</body>
</html>