-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoAuth.class.php
77 lines (76 loc) · 2.41 KB
/
oAuth.class.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
<?php
/**
* Простой класс для работы с oAuth в PLAINTEXT режиме
* @copyright 2011, CupIvan <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3
*/
class oAuth
{
private $params = array('oauth_signature_method' => 'PLAINTEXT');
/** конструктор */
function __construct($params = array())
{
$this->params = array_merge($this->params, $params);
}
/** получение параметра */
function __get($x)
{
return isset($this->params[$x]) ? $this->params[$x] : '';
}
/** временный токен */
function getRequestToken($uri)
{
$context = stream_context_create($this->getOpts($uri));
parse_str(@file_get_contents($uri, false, $context), $data);
$this->params = array_merge($this->params, $data);
return $data;
}
/** настоящий токен */
function getAccessToken($uri, $verifier)
{
$params['verifier'] = $verifier;
$context = stream_context_create($this->getOpts($uri));
parse_str(file_get_contents($uri, false, $context), $data);
$this->params = array_merge($this->params, $data);
return $data;
}
/** получение страницы */
public function get($uri)
{
$context = stream_context_create($this->getOpts($uri, 'GET'));
return @file_get_contents($uri, false, $context);
}
/** отправка данных */
public function post($uri, $data = '')
{
$context = stream_context_create($this->getOpts($uri, 'POST', $data));
return @file_get_contents($uri, false, $context);
}
/** генерация заголовков */
function getOpts($uri, $method = 'POST', $data = '')
{
if (is_array($data)) $data = http_build_query($data);
$data_len = strlen($data);
return array
(
'http' => array
(
'method' => $method,
'header' =>
"Connection: close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
'Authorization: OAuth '.
'realm="'.$uri.'",'.
'oauth_consumer_key="'.$this->oauth_consumer_key.'",'.
'oauth_signature_method="'.$this->oauth_signature_method.'",'.
'oauth_signature="'.urlencode($this->oauth_consumer_secret.'&'.$this->oauth_token_secret).'",'.
(!$this->oauth_token?'':'oauth_token="'.$this->oauth_token.'",').
(!$this->verifier?'':'oauth_verifier="'.$this->verifier.'",').
'oauth_nonce="'.uniqid('').'",'.
'oauth_timestamp="'.time().'",'.
'oauth_version="1.0"',
'content' => $data,
)
);
}
}