Skip to content

Commit 0ed8e55

Browse files
committed
Performance Improvements
Complete refactored Class (POST Calls Not Implemented Yet)
1 parent 84e6e48 commit 0ed8e55

File tree

1 file changed

+70
-63
lines changed

1 file changed

+70
-63
lines changed

TwitterOAuth.php

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ class TwitterOAuth
1010

1111
protected $config = array();
1212

13-
protected $params = array();
13+
protected $call = '';
14+
15+
protected $method = 'GET';
16+
17+
protected $getParams = array();
18+
19+
protected $postParams = array();
1420

1521

1622
public function __construct($config)
@@ -34,119 +40,137 @@ public function __construct($config)
3440
unset($defs, $filters);
3541
}
3642

37-
public function get($call, $params = null, $format = null)
43+
public function get($call, $getParams = null, $format = null)
3844
{
39-
if (!is_null($format)) {
40-
$this->format = $format;
41-
}
45+
$this->call = $call;
4246

43-
if (!is_null($params) && is_array($params)) {
44-
$this->params = $params;
47+
if ($getParams !== null && is_array($getParams)) {
48+
$this->getParams = $getParams;
4549
}
4650

47-
return $this->processRequest($call);
48-
}
51+
if ($format !== null) {
52+
$this->format = $format;
53+
}
4954

50-
protected function buildUrl($call)
51-
{
52-
return $this->url . $call . '.' . $this->format;
55+
return $this->sendRequest();
5356
}
5457

55-
protected function getUrl($call)
58+
protected function getParams($params)
5659
{
57-
$url = $this->buildUrl($call);
58-
59-
$params = $this->params;
60-
6160
$r = '';
6261

6362
ksort($params);
6463

6564
foreach ($params as $key => $value) {
66-
$r .= '&' . $key . '=' . $value;
65+
$r .= '&' . $key . '=' . urlencode($value);
6766
}
6867

6968
unset($params, $key, $value);
7069

71-
return $url . ((empty($r)) ? '' : '?') . $r;
70+
return trim($r, '&');
7271
}
7372

74-
protected function getOauthRequest()
73+
protected function getUrl($withParams = false)
74+
{
75+
$getParams = '';
76+
77+
if ($withParams === true) {
78+
$getParams = $this->getParams($this->getParams);
79+
80+
if (!empty($getParams)) {
81+
$getParams = '?' . $getParams;
82+
}
83+
}
84+
85+
return $this->url . $this->call . '.' . $this->format . $getParams;
86+
}
87+
88+
protected function getOauthParameters()
7589
{
7690
$time = time();
7791

7892
return array(
7993
'oauth_consumer_key' => $this->config['consumer_key'],
80-
'oauth_nonce' => $time,
94+
'oauth_nonce' => trim(base64_encode($time), '='),
8195
'oauth_signature_method' => 'HMAC-SHA1',
82-
'oauth_token' => $this->config['oauth_token'],
8396
'oauth_timestamp' => $time,
97+
'oauth_token' => $this->config['oauth_token'],
8498
'oauth_version' => '1.0'
8599
);
86100
}
87101

88-
protected function createBase($method, $call, $oauth)
102+
protected function getRequestString()
89103
{
90-
$url = $this->buildUrl($call);
104+
$params = array_merge($this->getParams, $this->postParams, $this->getOauthParameters());
91105

92-
$params = array_merge($this->params, $oauth);
106+
$params = $this->getParams($params);
93107

94-
$r = array();
95-
96-
ksort($params);
108+
return urlencode($params);
109+
}
97110

98-
foreach ($params as $key => $value) {
99-
$r[] = $key . '=' . rawurlencode($value);
100-
}
111+
protected function getSignatureBaseString()
112+
{
113+
$method = strtoupper($this->method);
101114

102-
unset($params, $key, $value);
115+
$url = urlencode($this->getUrl());
103116

104-
return $method . '&' . rawurlencode($url) . '&' . rawurlencode(implode('&', $r));
117+
return $method . '&' . $url . '&' . $this->getRequestString();
105118
}
106119

107-
protected function buildSignature($base)
120+
protected function getSigningKey()
108121
{
109-
$ckey = rawurlencode($this->config['consumer_secret']) . '&' .
110-
rawurlencode($this->config['oauth_token_secret']);
122+
return $this->config['consumer_secret'] . '&' . $this->config['oauth_token_secret'];
123+
}
111124

112-
return base64_encode(hash_hmac('sha1', $base, $ckey, true));
125+
protected function calculateSignature()
126+
{
127+
return base64_encode(hash_hmac('sha1', $this->getSignatureBaseString(), $this->getSigningKey(), true));
113128
}
114129

115-
protected function buildHeaders($oauth, $sign)
130+
protected function getOauthString()
116131
{
117-
$oauth = array_merge($oauth, array('oauth_signature' => $sign));
132+
$oauth = array_merge($this->getOauthParameters(), array('oauth_signature' => $this->calculateSignature()));
118133

119-
$r = 'Authorization: OAuth ';
134+
ksort($oauth);
120135

121136
$values = array();
122137

123138
foreach ($oauth as $key => $value) {
124139
$values[] = $key . '="' . rawurlencode($value) . '"';
125140
}
126141

127-
$r .= implode(', ', $values);
142+
$oauth = implode(', ', $values);
128143

129144
unset($values, $key, $value);
130145

146+
return $oauth;
147+
}
148+
149+
protected function buildRequestHeader()
150+
{
131151
return array(
132-
$r,
152+
'Authorization: OAuth ' . $this->getOauthString(),
133153
'Expect:'
134154
);
135155
}
136156

137-
protected function sendRequest($url, $headers, $postfields = null)
157+
protected function sendRequest()
138158
{
159+
$url = $this->getUrl(true);
160+
161+
$header = $this->buildRequestHeader();
162+
139163
$options = array(
140164
CURLOPT_URL => $url,
141165
CURLOPT_HEADER => false,
142-
CURLOPT_HTTPHEADER => $headers,
166+
CURLOPT_HTTPHEADER => $header,
143167
CURLOPT_RETURNTRANSFER => true,
144168
CURLOPT_SSL_VERIFYPEER => false,
145169
);
146170

147-
if (!is_null($postfields)) {
171+
/* if ($postfields !== null) {
148172
$options[CURLOPT_POSTFIELDS] = $postfields;
149-
}
173+
} */
150174

151175
$c = curl_init();
152176

@@ -160,21 +184,4 @@ protected function sendRequest($url, $headers, $postfields = null)
160184

161185
return $response;
162186
}
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-
}
180187
}

0 commit comments

Comments
 (0)