-
Notifications
You must be signed in to change notification settings - Fork 5
/
VkClient.php
99 lines (93 loc) · 3.22 KB
/
VkClient.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
<?php
require_once(__DIR__.'/lib/phpQuery/phpQuery/phpQuery.php');
require_once(__DIR__.'/lib/CurlHelper.php');
class VkClient
{
public $accessToken = null;
/**
* @param integer $appId VK Application ID
* @param string $email Email used to login
* @param string $password Password used to login
* @param integer $scope Access rights bit mask
* @param string $devNull URL to dummy file. File must exists and be visible to script.
* @throws Exception
*/
function __construct($appId, $email, $password, $scope, $devNull)
{
$query = array(
'client_id' => $appId,
'scope' => $scope,
'redirect_uri' => $devNull,
'display' => 'popup',
'response_type' => 'token',
'_hash' => 0,
);
$queryString = http_build_query($query);
$oauthPage = CurlHelper::getUrl("http://oauth.vk.com/oauth/authorize?$queryString", array(
CURLOPT_COOKIESESSION => true, // create new session
));
$phpQueryObject = phpQuery::newDocumentHTML($oauthPage);
if ($phpQueryObject->find("form input[name=email]")->count() == 1) {
$formData = array();
foreach ($phpQueryObject->find("form input") as $node) {
$jNode = pq($node);
$formData[ $jNode->attr('name') ] = $jNode->attr('value');
}
$formData['email'] = $email;
$formData['pass'] = $password;
$formAction = $phpQueryObject->find("form")->attr('action');
$cookieFile = tempnam(sys_get_temp_dir(), 'vkCookie');
$loginData = CurlHelper::postUrl($formAction, $formData, array(
CURLOPT_HEADER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIEJAR => $cookieFile,
CURLOPT_COOKIEFILE => $cookieFile,
));
if (preg_match('#access_token=(\w+)#', $loginData, $matches)) {
$this->accessToken = $matches[1];
unlink($cookieFile);
return;
} elseif (preg_match('#onclick\s*=\s*"\s*return\s+allow\s*\(\s*\);?\s*"#', $loginData, $matches)) {
// we need approve application
if (!preg_match('#function\s+allow\s*\(\s*\)\s*{.*?"(https://[^"]+)"#s', $loginData, $matches))
throw new Exception('can\'t get access token');
$loginData = CurlHelper::getUrl($matches[1], array(
CURLOPT_HEADER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIEJAR => $cookieFile,
CURLOPT_COOKIEFILE => $cookieFile,
));
if (preg_match('#access_token=(\w+)#', $loginData, $matches)) {
$this->accessToken = $matches[1];
unlink($cookieFile);
return;
}
} elseif ($phpQueryObject->find("form input[name=email]")->count() == 1) {
throw new Exception('wrong login/password');
}
}
throw new Exception('can\'t get access token');
}
/**
* @param string $apiName
* @param array $params
* @return mixed Contents of field response
* @throws Exception
*/
public function makeApiCall($apiName, $params) {
$query = array_merge(array(
'access_token' => $this->accessToken
), $params);
$queryString = http_build_query($query);
$answer = CurlHelper::getUrl("https://api.vk.com/method/$apiName?$queryString", array(
CURLOPT_SSL_VERIFYPEER => false,
));
$answer = json_decode($answer, true);
if (isset($answer['error']) || !isset($answer['response'])) {
throw new Exception(print_r($answer, true));
}
return $answer['response'];
}
}