-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtwitter_proxy.php
executable file
·122 lines (99 loc) · 4.14 KB
/
twitter_proxy.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Similar to Facebook Apps, you'll need to create a Twitter app first: https://apps.twitter.com/
*
* Code below from http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1 by Rivers
* with a few modfications by Mike Rogers to support variables in the URL nicely
*/
class TwitterProxy {
/**
* The tokens, keys and secrets from the app you created at https://dev.twitter.com/apps
*/
private $config = [
'use_whitelist' => true, // If you want to only allow some requests to use this script.
'base_url' => 'https://api.twitter.com/1.1/'
];
/**
* Only allow certain requests to twitter. Stop randoms using your server as a proxy.
*/
private $whitelist = [];
/**
* @param string $oauth_access_token OAuth Access Token ('Access token' on https://apps.twitter.com)
* @param string $oauth_access_token_secret OAuth Access Token Secret ('Access token secret' on https://apps.twitter.com)
* @param string $consumer_key Consumer key ('API key' on https://apps.twitter.com)
* @param string $consumer_secret Consumer secret ('API secret' on https://apps.twitter.com)
* @param string $user_id User id (http://gettwitterid.com/)
* @param string $screen_name Twitter handle
* @param string $count The number of tweets to pull out
*/
public function __construct($oauth_access_token, $oauth_access_token_secret, $consumer_key, $consumer_secret, $user_id, $screen_name, $count = 5) {
$this->config = array_merge($this->config, compact('oauth_access_token', 'oauth_access_token_secret', 'consumer_key', 'consumer_secret', 'user_id', 'screen_name', 'count'));
$this->whitelist['statuses/user_timeline.json?user_id=' . $this->config['user_id'] . '&screen_name=' . $this->config['screen_name'] . '&count=' . $this->config['count']] = true;
}
private function buildBaseString($baseURI, $method, $params) {
$r = [];
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
private function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = [];
foreach($oauth as $key => $value) {
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
$r .= implode(', ', $values);
return $r;
}
public function get($url) {
if (! isset($url)){
die('No URL set');
}
if ($this->config['use_whitelist'] && ! isset($this->whitelist[$url])){
die('URL is not authorised');
}
// Figure out the URL parameters
$url_parts = parse_url($url);
parse_str($url_parts['query'], $url_arguments);
$full_url = $this->config['base_url'] . $url; // URL with the query on it
$base_url = $this->config['base_url'] . $url_parts['path']; // URL without the query
// Set up the OAuth Authorization array
$oauth = [
'oauth_consumer_key' => $this->config['consumer_key'],
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $this->config['oauth_access_token'],
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
];
$base_info = $this->buildBaseString($base_url, 'GET', array_merge($oauth, $url_arguments));
$composite_key = rawurlencode($this->config['consumer_secret']) . '&' . rawurlencode($this->config['oauth_access_token_secret']);
$oauth['oauth_signature'] = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
// Make Requests
$header = [
$this->buildAuthorizationHeader($oauth),
'Expect:'
];
$options = [
CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $full_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
];
$feed = curl_init();
curl_setopt_array($feed, $options);
$result = curl_exec($feed);
$info = curl_getinfo($feed);
curl_close($feed);
// Send suitable headers to the end user.
if (isset($info['content_type']) && isset($info['size_download'])){
header('Content-Type: ' . $info['content_type']);
header('Content-Length: ' . $info['size_download']);
}
return $result;
}
}