Skip to content

Instantly share code, notes, and snippets.

@miisa55
Last active November 19, 2020 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miisa55/a91ab8e91ab2b4ac4eb6f6f2b1b34c9f to your computer and use it in GitHub Desktop.
Save miisa55/a91ab8e91ab2b4ac4eb6f6f2b1b34c9f to your computer and use it in GitHub Desktop.
APIを利用した埋め込みTwitter
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width">
<style>
body {
font-family: 'Hiragino Maru Gothic ProN',
'Meiryo', 'Noto Sans CJK', sans-serif;
color: #3f3f3f;
margin: 0;
padding: 0;
}
@font-face {
font-display: swap;
font-family: 'Nyashi'; // フォントを指定
src: url(font/Nyashi.woff2) format('woff2'); // フォントファイルを指定
}
.wf-active body, #results-name {
font-family: 'Nyashi';
}
a {
text-decoration: none;
}
a:hover, a:active {
background-color: rgba(180,180,180,0.5);
}
.twitter-header, .twitter-created, .twitter-footer {
font-size: 4.5vw;
}
.tweet {
border-bottom: .3vw solid #999;
margin: 0 0 3vw 0;
padding: 0 0 1vw 0;
}
.twitter-header {
border-bottom: .3vw dotted #999;
color: #666;
margin: 0 0 1.5vw 0;
padding: 0 0 1vw 0;
}
.twitter-s-name, .count-block {
font-size: 5vw;
}
.user-block {
display: flex;
position: relative;
}
.twitter-icon {
margin-right: 2vw;
}
.twitter-icon img {
width: 14vw;
height: 14w;
border-radius: 50%;
}
.twitter-name {
font-size: 5vw;
color: #111;
}
.twitter-s-name {
color: #666;
}
.twitter-created {
position: absolute;
top: .5vw;
right: 1vw;
}
.twitter-body {
padding: 3vw 2vw 0 2vw;
}
.twitter-text {
margin: 0 0 3vw 0;
font-size: 5.3vw;
line-height: 150%;
}
.twitter-text a, .twitter-perma a, .twitter-footer a {
color: #ff69b4;
}
.twitter-img {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
}
.twitter-img img {
display: block;
width: 46vw;
margin: 0 2vw 2vw 0;
}
video {
width: 94vw;
margin: 0 2vw 2vw 0;
}
.count-block {
display: flex;
}
.twitter-f-count, .twitter-r-count, .twitter-perma {
line-height: 100%;
}
.twitter-f-count, .twitter-r-count {
width: 24vw;
height: 5vw;
padding: .7vw 0 0 5.5vw;
}
.twitter-f-count {
background: url(img/heart.svg) 0 .9vw / 4.5vw no-repeat;
}
.twitter-r-count {
background: url(img/retweet.svg) 0 .8vw / 4.8vw no-repeat;
}
.twitter-perma {
display: flex;
justify-content: flex-end;
width: 36vw;
}
.tt-icon {
display: block;
width: 5vw;
height: 5vw;
background: url(img/twitter-timeline.svg) 0 50% / 5.5vw no-repeat;
}
.back-text {
display: none;
}
.twitter-footer {
display: flex;
justify-content: flex-end;
}
</style>
</head>
<body ontouchstart="">
<?php
require 'twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerKey = 'コンシューマーAPIキー';
$consumerSecret = 'コンシューマーAPIシークレットキー';
$accessToken = 'アクセストークン';
$accessTokenSecret = 'アクセストークンシークレット';
$conn = new TwitterOAuth( $consumerKey, $consumerSecret, $accessToken, $accessTokenSecret );
// 10 件取得
$users_params = [
'screen_name' => '@を付けないツイッターID',
'count' => '表示件数',
'tweet_mode' => 'extended',
];
$res = $conn->get( 'statuses/user_timeline', $users_params );
$arr = json_decode ( json_encode( $res ), true );
echo '<div class="twitter-header">ツイート10件、スクロールできちゃう</div>';
foreach( $arr as $i ) {
$Text = nl2br( $i['full_text'] );
$Icon = str_replace( "_normal.", ".", $i['user']['profile_image_url_https'] );
if( empty( $Icon ) ) {
$i['user']['default_profile_image_https'];
}
$Name = $i['user']['name'];
$ScName = $i['user']['screen_name'];
$URL = $i['entities']['urls'];
foreach( $URL as $key ) {
$Text = str_replace( $key['url'], '<a href="'.$key['expanded_url'].'" target="b_lank">'.$key['display_url'].'</a>', $Text );
}
unset( $URL );
$Tag = $i['entities']['hashtags'];
foreach( $Tag as $key ) {
$Text = str_replace( "#".$key['text'], '<a href="https://twitter.com/hashtag/'.$key['text'].'?src=hashtag_click" target="b_lank">#'.$key['text'].'</a>', $Text );
}
unset( $Tag );
$Mention = $i['entities']['user_mentions'];
foreach( $Mention as $key ) {
$Text = str_replace( "@".$key['screen_name'], '<a href="https://twitter.com/'.$key['screen_name'].'" target="b_lank">@'.$key['screen_name'].'</a>', $Text );
}
unset( $Mention );
$Media = $i['entities']['media'];
if( !empty( $Media ) ) {
foreach( $Media as $key ) {
$Text = str_replace( $key['url'], ' ', $Text );
}
}
unset( $Media );
$ImgUrl = $i['extended_entities']['media'];
$VideoUrl = $i['extended_entities']['media'][0]['video_info']['variants'][0]['url'];
$VideoThumb = $i['extended_entities']['media'][0]['media_url_https'];
$Fcount = $i['favorite_count'];
$Rcount = $i['retweet_count'];
$PermaID = $i['id_str'];
$TweetTime = strtotime( $i['created_at'] );
$NowTime = time();
$RelaTime = $NowTime - $TweetTime;
if( $RelaTime < 60 ) {
$TweetTime = $RelaTime . '秒';
}
elseif( $RelaTime >= 60 && $RelaTime < ( 60 * 60 ) ) {
$TweetTime = floor( $RelaTime / 60 ) . '分';
}
elseif( $RelaTime >= ( 60 * 60 ) && $RelaTime < ( 60 * 60 * 24 ) ) {
$TweetTime = floor( $RelaTime / ( 60 * 60 ) ) . '時間';
}
elseif( $RelaTime >= ( 60 * 60 * 24) ) {
$TweetTime = date( 'n月j日', $TweetTime );
}
elseif( $RelaTime >= ( 60 * 60 * 24 * 365 ) ) {
$TweetTime = date( 'Y年n月j日', $TweetTime );
}
echo '<div class="tweet">';
echo '<div class="user-block">';
echo '<a href="https://twitter.com/'.$ScName.'" target="b_lank"><div class="twitter-icon"><img alt="'.$Name.'" src="'.$Icon.'"></div></a>';
echo '<a href="https://twitter.com/'.$ScName.'" target="b_lank">';
echo '<div class="name-block">';
echo '<div class="twitter-name">'.$Name.'</div>';
echo '<div class="twitter-s-name">@'.$ScName.'</div>';
echo '</div>';
echo '</a>';
echo '<div class="twitter-created">'.$TweetTime.'</div>';
echo '</div>';
echo '<div class="twitter-body">';
echo '<div class="twitter-text">'.$Text.'</div>';
if( empty( $VideoUrl ) ) {
if( is_array( $ImgUrl ) ) {
echo '<div class="twitter-img">';
foreach( $ImgUrl as $key ) {
echo '<a href="'.$key['media_url_https'].':orig" target="b_lank"><img src="'.$key['media_url_https'].'"></a>';
}
echo '</div>';
}
}
unset( $ImgUrl );
if( empty( $VideoUrl ) ) {
} else {
echo '<video src="'.$VideoUrl.'" width="auto" height="auto" controls loop preload="metadata" poster="'.$VideoThumb.'"></video>';
}
echo '<div class="count-block">';
echo '<div class="twitter-f-count">'.$Fcount.'</div>';
echo '<div class="twitter-r-count">'.$Rcount.'</div>';
echo '<div class="twitter-perma"><a href="https://twitter.com/'.$ScName.'/status/'.$PermaID.'" target="b_lank" class="tt-icon"><span class="back-text">表示</span></a></div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
unset( $arr );
echo '<div class="twitter-footer">by&nbsp;<a href="https://twitter.com/" target="b_lank">Twitter</a>,&nbsp;for&nbsp;<a href="https://wp.miisa.pink/" target="b_lank">MiisaGOGO!</a></div>';
?>
<script>
// Web Font Loader
WebFontConfig = {
custom: { families: [ 'Nyashi' ] }, // フォントを指定
active: function () {}
};
(function () {
var wf = document.createElement( 'script' );
wf.src = 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName( 'script' ) [0];
s.parentNode.insertBefore( wf, s );
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment