/
twitter_callback.php
105 lines (81 loc) · 2.95 KB
/
twitter_callback.php
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
<?php
include("generate_tweet.php");
require 'twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
session_start();
$config = require_once 'config.php';
// get and filter oauth verifier
$oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier');
// check tokens
if (empty($oauth_verifier) || empty($_SESSION['oauth_token']) || empty($_SESSION['oauth_token_secret'])) {
// something's missing, go and login again
header('Location: ' . $config['url_login']);
}
// connect with application token
$connection = new TwitterOAuth(
$config['consumer_key'],
$config['consumer_secret'],
$_SESSION['oauth_token'],
$_SESSION['oauth_token_secret']
);
// request user token
$token = $connection->oauth(
'oauth/access_token', [
'oauth_verifier' => $oauth_verifier
]
);
// connect with user token
$twitter = new TwitterOAuth(
$config['consumer_key'],
$config['consumer_secret'],
$token['oauth_token'],
$token['oauth_token_secret']
);
$user = $twitter->get('account/verify_credentials');
// if something's wrong, go and log in again
if(isset($user->error)) {
header('Location: ' . $config['url_login']);
}
$tweet_to_post = getTweet();
// post a tweet
$status = $twitter->post(
"statuses/update", [
"status" => $tweet_to_post
]
);
// echo ('Created new status successfully <br/>' . $tweet_to_post);
// echo("<br/><a href = 'http://dev.colinroitt.uk/spicytake'><h3>Link Home</h3></a>");
?>
<html>
<head>
<link type="text/css" href="./style.css" rel="stylesheet">
</head>
<body>
<div class='center-post-tweet'>
<h1>Here is your <em>SPICY</em> hot take</h1>
<h3>It's been posted to your Twitter for you so you don't have to</h3>
<div id="tweet" tweetID=<?php echo($status->id) ?> ></div>
<script sync src="https://platform.twitter.com/widgets.js"></script>
<script>
window.onload = (function(){
var tweet = document.getElementById("tweet");
var id = tweet.getAttribute("tweetID");
console.log(id);
twttr.widgets.createTweet(
id, tweet,
{
conversation : 'none', // or all
cards : 'hidden', // or visible
linkColor : '#cc0000', // default is blue
theme : 'light' // or dark
})
.then (function (el) {
el.contentDocument.querySelector(".footer").style.display = "none";
});
});
</script>
<a href="http://dev.colinroitt.uk/spicytake"><h2 id='try-again'>Try Aagin</h2></a>
<a href="http://colinroitt.uk/">ColinRoitt.uk</a> <a href="http://blog.colinroitt.uk/2017/08/12/today-i-brought-to-life-the-stupidest-web-thing-ive-ever-made-spicytake/#more-186">About This</a>
</div>
</body>
</html>