<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ツイッタータイムライン取得</title>
<style type="text/css">
.twit_box {
height: auto;
width: 200px;
border: 1px solid #CCC;
border-radius: 5px; /* CSS3草案 */
-webkit-border-radius: 5px; /* Safari,Google Chrome用 */
-moz-border-radius: 5px;
text-align: center;
box-shadow: 0 2px 1px #eee;
margin: 5px;
padding: 3px;
}
.twit_box img {
max-width: 200px;
max-height: 150px;
text-align: center;
}
.user_box {
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: solid;
border-right-style: none;
border-bottom-style: none;
border-left-style: none;
border-top-color: #999;
margin: 2px;
padding: 2px;
font-size: 12px;
color: #CCC;
}
body {
width: 80%;
}
</style>
</head>
<body>
<h1>これはTwitterAPIでのタイムライン読み込みテストです。</h1>
<h2>記事元:</h2>
<div id="content">
<?php
//ini_set( 'display_errors', 1 );
$loop_count = "30"; //取得数
$tweetp = "#全日本もう帰りたい協会 -rt"; //検索文字
//// OAuthライブラリの読込
require_once("twitteroauth/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerKey = "取得したconsumerKey(´・ω・)ス";
$consumerSecret = "取得したconsumerSecret(´・ω・)ス";
$accessToken = "取得したaccessToken(´・ω・)ス";
$accessTokenSecret = "取得したaccessTokenSecret(´・ω・)ス";
//$consumerKey = "取得したconsumerKey";
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$result = $connection->get("search/tweets", array("q" => $tweetp , 'count' => $loop_count));
$tweets_arr = json_decode(json_encode($result), true );
for($i = 0; $i < $loop_count; $i++) {
$user_name = $tweets_arr['statuses'][$i]['user']['name']; //ユーザー名
$user_id = $tweets_arr['statuses'][$i]['user']['screen_name']; //ユーザーID
$twit_id = $tweets_arr['statuses'][$i]['id_str']; //ツイートID
$img = $tweets_arr['statuses'][$i]['entities']['media']['0']['media_url']; //投稿画像
$user_img = $tweets_arr['statuses'][$i]['user']['profile_image_url']; //ユーザープロフィール画像
$url = "https://twitter.com/$user_id/status/$twit_id"; //ツイートIDのリンク
//表示開始
//表示開始
echo '<div class="twit_box">';
echo "<a href=\"$url\">";
if(isset($img) and empty($is_rt)){
echo "<img src=\"$img\" border=\"0\"/>";
}
echo '<div class="user_box">';
echo "<img src=\"$user_img\" border=\"0\"/>"; //プロフィール画像の表示
echo $user_name;
echo '</div>';
echo $tweets_arr['statuses'][$i]['text'];
echo '</a>';
echo '</div>';
// 表示ここまで
}
//デバック
function table($val){
if(!$val) echo 'table error';
if(is_array($val)){
echo '<table border="1" cellspacing="0" bordercolor="#333333" >';
_table($val);
echo '</table>';
}else{
echo var_dump($val);
}
}
function _table($val){
echo '<tr align="left" valign="top">'.chr(10);
echo ' <td align="left" valign="top" bgcolor="#33EE33">key</td>'.chr(10);
echo ' <td align="left" valign="top" bgcolor="#33EE33">value</td>'.chr(10);
echo '</tr>'.chr(10);
foreach($val as $key => $_val){
echo '<tr align="left" valign="top">'.chr(10);
echo ' <td align="left" valign="top" bgcolor="#FFFFFF">'.$key.'</td>'.chr(10);
echo ' <td align="left" valign="top" bgcolor="#FFFFFF">'.chr(10);
if(is_array($_val)) table($_val);
else echo $_val.'</td>'.chr(10).'</tr>'.chr(10);
}
}
//階層表示確認(´・ω・)ス
//include_once("../dBug.php");
//new dBug($tweets_arr);
//table($result);
?>
</div>
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="masonry.pkgd.min.js"></script>
<script>
$(function(){
$('#content').masonry({ //敷き詰めたい要素を囲うidなど指定。
itemSelector : '.twit_box' //敷き詰めたい要素のclassを指定。
});
});
</script>
</body>
</html>