-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
205 lines (185 loc) · 7.06 KB
/
index.php
File metadata and controls
205 lines (185 loc) · 7.06 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* GETメソッドのリクエスト [ベアラートークン]
* https://syncer.jp/twitter-api-matome
* 2015-09-01
*/
// 設定
$bearer_token = '' ; // ベアラートークン
$request_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json' ; // エンドポイント
// パラメータ
$params = array(
'screen_name' => '@nojima_tsuyoshi' ,
'count' => 10 ,
) ;
// パラメータがある場合
if( $params )
{
$request_url .= '?' . http_build_query( $params ) ;
}
// リクエスト用のコンテキスト
$context = array(
'http' => array(
'method' => 'GET' , // リクエストメソッド
'header' => array( // ヘッダー
'Authorization: Bearer ' . $bearer_token ,
) ,
) ,
) ;
// 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_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()]を使うには下記の通りです…
// $json = @file_get_contents( $request_url , false , stream_context_create( $context ) ) ;
// JSONをオブジェクトに変換
$obj = json_decode( $json ) ;
// HTML用
$html = '' ;
// エラー判定
if( !$json || !$obj )
{
$html .= '<h2>エラー内容</h2>' ;
$html .= '<p>データを取得することができませんでした…。設定を見直して下さい。</p>' ;
}
// 検証用にレスポンスヘッダーを出力 [本番環境では不要]
$html .= '<h2>取得したデータ</h2>' ;
$html .= '<p>下記のデータを取得できました。</p>' ;
$html .= '<h3>ボディ(JSON)</h3>' ;
$html .= '<p><textarea rows="8">' . $json . '</textarea></p>' ;
$html .= '<h3>レスポンスヘッダー</h3>' ;
$html .= '<p><textarea rows="8">' . $header . '</textarea></p>' ;
/**
* substr_replace をマルチバイト文字列に対応
* https://gist.github.com/stemar/8287074
* 2015-02-17
*/
function mb_substr_replace($string, $replacement, $start, $length=NULL) {
if (is_array($string)) {
$num = count($string);
// $replacement
$replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
// $start
if (is_array($start)) {
$start = array_slice($start, 0, $num);
foreach ($start as $key => $value)
$start[$key] = is_int($value) ? $value : 0;
}
else {
$start = array_pad(array($start), $num, $start);
}
// $length
if (!isset($length)) {
$length = array_fill(0, $num, 0);
}
elseif (is_array($length)) {
$length = array_slice($length, 0, $num);
foreach ($length as $key => $value)
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
}
else {
$length = array_pad(array($length), $num, $length);
}
// Recursive call
return array_map(__FUNCTION__, $string, $replacement, $start, $length);
}
preg_match_all('/./us', (string)$string, $smatches);
preg_match_all('/./us', (string)$replacement, $rmatches);
if ($length === NULL) $length = mb_strlen($string);
array_splice($smatches[0], $start, $length, $rmatches[0]);
return join($smatches[0]);
}
/**
* Twitter APIから取得したツイート情報のurls、user_mentions、hashtagsをリンクに変換
* http://stackoverflow.com/questions/11533214/php-how-to-use-the-twitter-apis-data-to-convert-urls-mentions-and-hastags-in
* 2014-08-26
*/
function json_tweet_text_to_HTML($tweet, $links=true, $users=true, $hashtags=true)
{
$return = $tweet->text;
// echo $tweet->text;
$entities = array();
if($links && is_array($tweet->entities->urls))
{
foreach($tweet->entities->urls as $e)
{
$temp["start"] = $e->indices[0];
$temp["end"] = $e->indices[1];
$temp["replacement"] = "<a href='".$e->expanded_url."' target='_blank'>".$e->display_url."</a>";
$entities[] = $temp;
}
}
if($users && is_array($tweet->entities->user_mentions))
{
foreach($tweet->entities->user_mentions as $e)
{
$temp["start"] = $e->indices[0];
$temp["end"] = $e->indices[1];
$temp["replacement"] = "<a href='https://twitter.com/".$e->screen_name."' target='_blank'>@".$e->screen_name."</a>";
$entities[] = $temp;
}
}
if($hashtags && is_array($tweet->entities->hashtags))
{
foreach($tweet->entities->hashtags as $e)
{
$temp["start"] = $e->indices[0];
$temp["end"] = $e->indices[1];
$temp["replacement"] = "<a href='https://twitter.com/hashtag/".$e->text."?src=hash' target='_blank'>#".$e->text."</a>";
$entities[] = $temp;
}
}
usort($entities, function($a,$b){return($b["start"]-$a["start"]);});
foreach($entities as $item)
{
//$return = substr_replace($return, $item["replacement"], $item["start"], $item["end"] - $item["start"]);
$return = mb_substr_replace($return, $item["replacement"], $item["start"], $item["end"] - $item["start"]);
}
return($return);
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Twitter API 1.1 ベアラートークンによるフィード取得</title>
<script type="text/javascript">
var timerStart = Date.now();
</script>
</head>
<body>
<h1>Twitter API 1.1 ベアラートークンによるフィード取得</h1>
<?php echo $html; ?>
<h2>出力サンプル</h2>
<ul>
<?php
foreach ($obj as $key => $value) {
echo '<li>';
//投稿日時
$tweetCreatedAt = date('Y/m/d', strtotime($value->created_at));
//ツイートのアドレス
$tweetUrl = 'https://twitter.com/' . $value->user->screen_name . '/status/' . $value->id;
//ツイート本文
$tweetText = json_tweet_text_to_HTML($value);
echo '<a href="' . $tweetUrl . '" target="_blank">' . $tweetCreatedAt . '</a> ' . $tweetText . '<br>';
echo '</li>';
}
?>
</ul>
<h2>オブジェクト情報</h2>
<pre>
<?php print_r($obj); ?>
</pre>
</body>
</html>