@@ -10,7 +10,13 @@ class TwitterOAuth
10
10
11
11
protected $ config = array ();
12
12
13
- protected $ params = array ();
13
+ protected $ call = '' ;
14
+
15
+ protected $ method = 'GET ' ;
16
+
17
+ protected $ getParams = array ();
18
+
19
+ protected $ postParams = array ();
14
20
15
21
16
22
public function __construct ($ config )
@@ -34,119 +40,137 @@ public function __construct($config)
34
40
unset($ defs , $ filters );
35
41
}
36
42
37
- public function get ($ call , $ params = null , $ format = null )
43
+ public function get ($ call , $ getParams = null , $ format = null )
38
44
{
39
- if (!is_null ($ format )) {
40
- $ this ->format = $ format ;
41
- }
45
+ $ this ->call = $ call ;
42
46
43
- if (! is_null ( $ params ) && is_array ($ params )) {
44
- $ this ->params = $ params ;
47
+ if ($ getParams !== null && is_array ($ getParams )) {
48
+ $ this ->getParams = $ getParams ;
45
49
}
46
50
47
- return $ this ->processRequest ($ call );
48
- }
51
+ if ($ format !== null ) {
52
+ $ this ->format = $ format ;
53
+ }
49
54
50
- protected function buildUrl ($ call )
51
- {
52
- return $ this ->url . $ call . '. ' . $ this ->format ;
55
+ return $ this ->sendRequest ();
53
56
}
54
57
55
- protected function getUrl ( $ call )
58
+ protected function getParams ( $ params )
56
59
{
57
- $ url = $ this ->buildUrl ($ call );
58
-
59
- $ params = $ this ->params ;
60
-
61
60
$ r = '' ;
62
61
63
62
ksort ($ params );
64
63
65
64
foreach ($ params as $ key => $ value ) {
66
- $ r .= '& ' . $ key . '= ' . $ value ;
65
+ $ r .= '& ' . $ key . '= ' . urlencode ( $ value) ;
67
66
}
68
67
69
68
unset($ params , $ key , $ value );
70
69
71
- return $ url . (( empty ( $ r )) ? '' : ' ? ' ) . $ r ;
70
+ return trim ( $ r , ' & ' ) ;
72
71
}
73
72
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 ()
75
89
{
76
90
$ time = time ();
77
91
78
92
return array (
79
93
'oauth_consumer_key ' => $ this ->config ['consumer_key ' ],
80
- 'oauth_nonce ' => $ time ,
94
+ 'oauth_nonce ' => trim ( base64_encode ( $ time), ' = ' ) ,
81
95
'oauth_signature_method ' => 'HMAC-SHA1 ' ,
82
- 'oauth_token ' => $ this ->config ['oauth_token ' ],
83
96
'oauth_timestamp ' => $ time ,
97
+ 'oauth_token ' => $ this ->config ['oauth_token ' ],
84
98
'oauth_version ' => '1.0 '
85
99
);
86
100
}
87
101
88
- protected function createBase ( $ method , $ call , $ oauth )
102
+ protected function getRequestString ( )
89
103
{
90
- $ url = $ this ->buildUrl ( $ call );
104
+ $ params = array_merge ( $ this ->getParams , $ this -> postParams , $ this -> getOauthParameters () );
91
105
92
- $ params = array_merge ( $ this ->params , $ oauth );
106
+ $ params = $ this ->getParams ( $ params );
93
107
94
- $ r = array ();
95
-
96
- ksort ($ params );
108
+ return urlencode ($ params );
109
+ }
97
110
98
- foreach ( $ params as $ key => $ value ) {
99
- $ r [] = $ key . ' = ' . rawurlencode ( $ value );
100
- }
111
+ protected function getSignatureBaseString ()
112
+ {
113
+ $ method = strtoupper ( $ this -> method );
101
114
102
- unset( $ params , $ key , $ value );
115
+ $ url = urlencode ( $ this -> getUrl () );
103
116
104
- return $ method . '& ' . rawurlencode ( $ url) . '& ' . rawurlencode ( implode ( ' & ' , $ r ) );
117
+ return $ method . '& ' . $ url . '& ' . $ this -> getRequestString ( );
105
118
}
106
119
107
- protected function buildSignature ( $ base )
120
+ protected function getSigningKey ( )
108
121
{
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
+ }
111
124
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 ));
113
128
}
114
129
115
- protected function buildHeaders ( $ oauth , $ sign )
130
+ protected function getOauthString ( )
116
131
{
117
- $ oauth = array_merge ($ oauth , array ('oauth_signature ' => $ sign ));
132
+ $ oauth = array_merge ($ this -> getOauthParameters () , array ('oauth_signature ' => $ this -> calculateSignature () ));
118
133
119
- $ r = ' Authorization: OAuth ' ;
134
+ ksort ( $ oauth ) ;
120
135
121
136
$ values = array ();
122
137
123
138
foreach ($ oauth as $ key => $ value ) {
124
139
$ values [] = $ key . '=" ' . rawurlencode ($ value ) . '" ' ;
125
140
}
126
141
127
- $ r . = implode (', ' , $ values );
142
+ $ oauth = implode (', ' , $ values );
128
143
129
144
unset($ values , $ key , $ value );
130
145
146
+ return $ oauth ;
147
+ }
148
+
149
+ protected function buildRequestHeader ()
150
+ {
131
151
return array (
132
- $ r ,
152
+ ' Authorization: OAuth ' . $ this -> getOauthString () ,
133
153
'Expect: '
134
154
);
135
155
}
136
156
137
- protected function sendRequest ($ url , $ headers , $ postfields = null )
157
+ protected function sendRequest ()
138
158
{
159
+ $ url = $ this ->getUrl (true );
160
+
161
+ $ header = $ this ->buildRequestHeader ();
162
+
139
163
$ options = array (
140
164
CURLOPT_URL => $ url ,
141
165
CURLOPT_HEADER => false ,
142
- CURLOPT_HTTPHEADER => $ headers ,
166
+ CURLOPT_HTTPHEADER => $ header ,
143
167
CURLOPT_RETURNTRANSFER => true ,
144
168
CURLOPT_SSL_VERIFYPEER => false ,
145
169
);
146
170
147
- if (! is_null ( $ postfields) ) {
171
+ /* if ($postfields !== null ) {
148
172
$options[CURLOPT_POSTFIELDS] = $postfields;
149
- }
173
+ } */
150
174
151
175
$ c = curl_init ();
152
176
@@ -160,21 +184,4 @@ protected function sendRequest($url, $headers, $postfields = null)
160
184
161
185
return $ response ;
162
186
}
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
187
}
0 commit comments