forked from nanexcool/yo-php
-
Notifications
You must be signed in to change notification settings - Fork 5
/
yo.php
83 lines (62 loc) · 1.85 KB
/
yo.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
<?php
class Yo {
protected $_apiKey;
private $urls = array(
'all' => 'http://api.justyo.co/yoall/',
'user' => 'http://api.justyo.co/yo/',
'subscribers' => 'http://api.justyo.co/subscribers_count/'
);
public function __construct($apiKey) {
$this->_apiKey = $apiKey;
}
public function user($username) {
if ($username != '') {
$this->processRequest($username);
}
}
public function all() {
$this->processRequest();
}
public function subscribers() {
$url = $this->urls['subscribers'];
$url .= '?api_token=' . $this->_apiKey;
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
curl_close($curl);
// Result is in format {"result": 1 }
$result = json_decode($result, true);
if (isset($result['result'])) {
return $result['result'];
}
return false;
}
private function processRequest($username = '') {
$postFields = array(
'api_token' => $this->_apiKey
);
$url = $this->urls['all'];
if ($username != '') {
$postFields['username'] = $username;
$url = $this->urls['user'];
}
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postFields
);
$result = '';
$curl = curl_init();
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}