Skip to content

Commit 8b17fa6

Browse files
committed
Add TwitterOAuth ( POST calls not tested - not a stable version)
1 parent c509518 commit 8b17fa6

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

TwitterOAuth.php

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
3+
namespace TwitterOAuth;
4+
5+
class TwitterOAuth
6+
{
7+
protected $url = 'https://api.twitter.com/1.1/';
8+
9+
protected $format = 'json';
10+
11+
protected $config = array();
12+
13+
protected $params = array();
14+
15+
16+
public function __construct($config)
17+
{
18+
$defs = array(
19+
'consumer_key' => '',
20+
'consumer_secret' => '',
21+
'oauth_token' => '',
22+
'oauth_token_secret' => '',
23+
);
24+
25+
$filters = array(
26+
'consumer_key' => FILTER_SANITIZE_STRING,
27+
'consumer_secret' => FILTER_SANITIZE_STRING,
28+
'oauth_token' => FILTER_SANITIZE_STRING,
29+
'oauth_token_secret' => FILTER_SANITIZE_STRING,
30+
);
31+
32+
$this->config = filter_var_array(array_merge($defs, $config), $filters);
33+
34+
unset($defs, $filters);
35+
}
36+
37+
public function get($call, $params = null, $format = null)
38+
{
39+
if (!is_null($format)) {
40+
$this->format = $format;
41+
}
42+
43+
if (!is_null($params) && is_array($params)) {
44+
$this->params = $params;
45+
}
46+
47+
return $this->processRequest($call);
48+
}
49+
50+
protected function buildUrl($call)
51+
{
52+
return $this->url . $call . '.' . $this->format;
53+
}
54+
55+
protected function getUrl($call)
56+
{
57+
$url = $this->buildUrl($call);
58+
59+
$params = $this->params;
60+
61+
$r = '';
62+
63+
ksort($params);
64+
65+
foreach ($params as $key => $value) {
66+
$r .= '&' . $key . '=' . $value;
67+
}
68+
69+
unset($params, $key, $value);
70+
71+
return $url . ((empty($r)) ? '' : '?') . $r;
72+
}
73+
74+
protected function getOauthRequest()
75+
{
76+
$time = time();
77+
78+
return array(
79+
'oauth_consumer_key' => $this->config['consumer_key'],
80+
'oauth_nonce' => $time,
81+
'oauth_signature_method' => 'HMAC-SHA1',
82+
'oauth_token' => $this->config['oauth_token'],
83+
'oauth_timestamp' => $time,
84+
'oauth_version' => '1.0'
85+
);
86+
}
87+
88+
protected function createBase($method, $call, $oauth)
89+
{
90+
$url = $this->buildUrl($call);
91+
92+
$params = array_merge($this->params, $oauth);
93+
94+
$r = array();
95+
96+
ksort($params);
97+
98+
foreach ($params as $key => $value) {
99+
$r[] = $key . '=' . rawurlencode($value);
100+
}
101+
102+
unset($params, $key, $value);
103+
104+
return $method . '&' . rawurlencode($url) . '&' . rawurlencode(implode('&', $r));
105+
}
106+
107+
protected function buildSignature($base)
108+
{
109+
$ckey = rawurlencode($this->config['consumer_secret']) . '&' .
110+
rawurlencode($this->config['oauth_token_secret']);
111+
112+
return base64_encode(hash_hmac('sha1', $base, $ckey, true));
113+
}
114+
115+
protected function buildHeaders($oauth, $sign)
116+
{
117+
$oauth = array_merge($oauth, array('oauth_signature' => $sign));
118+
119+
$r = 'Authorization: OAuth ';
120+
121+
$values = array();
122+
123+
foreach ($oauth as $key => $value) {
124+
$values[] = $key . '="' . rawurlencode($value) . '"';
125+
}
126+
127+
$r .= implode(', ', $values);
128+
129+
unset($values, $key, $value);
130+
131+
return array(
132+
$r,
133+
'Expect:'
134+
);
135+
}
136+
137+
protected function sendRequest($url, $headers, $postfields = null)
138+
{
139+
$options = array(
140+
CURLOPT_URL => $url,
141+
CURLOPT_HEADER => false,
142+
CURLOPT_HTTPHEADER => $headers,
143+
CURLOPT_RETURNTRANSFER => true,
144+
CURLOPT_SSL_VERIFYPEER => false,
145+
);
146+
147+
if (!is_null($postfields)) {
148+
$options[CURLOPT_POSTFIELDS] = $postfields;
149+
}
150+
151+
$c = curl_init();
152+
153+
curl_setopt_array($c, $options);
154+
155+
$response = curl_exec($c);
156+
157+
curl_close($c);
158+
159+
unset($options, $c);
160+
161+
return $response;
162+
}
163+
164+
protected function processRequest($call, $method = 'GET', $postfields = null)
165+
{
166+
$url = $this->getUrl($call);
167+
168+
$oauth = $this->getOauthRequest();
169+
170+
$base = $this->createBase($method, $call, $oauth);
171+
172+
$sign = $this->buildSignature($base);
173+
174+
$headers = $this->buildHeaders($oauth, $sign);
175+
176+
unset($oauth, $base, $sign);
177+
178+
return $this->sendRequest($url, $headers, $postfields);
179+
}
180+
}

0 commit comments

Comments
 (0)