forked from ivansky/php-yandex-wordstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YADWordstat.php
171 lines (134 loc) · 4.25 KB
/
YADWordstat.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
class YADWordstat {
const JSON_URL = 'https://api.direct.yandex.ru/v4/json/';
public $login;
private static $redis = null;
public $errors = array(
30 => 'Массив слов пустой',
31 => 'Попытка создать 6-ой отчет',
56 => 'Превышен лимит запросов',
71 => 'Параметры запроса указаны неверно',
152 => 'Не достаточно баллов'
);
private $request = array('locale' => 'ru');
private $wait = array();
private $complete = array();
public static function redis(Redis &$__redis){
self::$redis = $__redis;
}
public function __construct($login, $certdir){
$this->login = $login;
$this->certdir = $certdir;
$this->request['login'] = $this->login;
}
public function units(){
$crc = crc32(sprintf('yad_units_%s', $this->login));
if(self::$redis && $units = self::$redis->get($crc)){
return $units;
}
$this->request['method'] = 'GetClientsUnits';
$this->request['param'] = array($this->login);
$this->request['login'] = $this->login;
$response = $this->request();
$units = (int)$response->data[0]->UnitsRest;
if(self::$redis){
self::$redis->set($crc, $units, 3600 * 2);
}
return $units;
}
public function getReportList(){
$this->request['method'] = 'GetWordstatReportList';
$this->request['param'] = array();
$this->request['login'] = $this->login;
$response = $this->request();
if(isset($response->data)){
return $response->data;
}
return false;
}
public function getReport($id){
$this->request['method'] = 'GetWordstatReport';
$this->request['param'] = (int)$id;
$response = $this->request();
if(isset($response->data)){
return $response->data;
}
return false;
}
public function getReportInfo($id){
$report = $this->getReport($id);
$data = $report->data;
}
public function deleteReport($id){
$this->request['method'] = 'DeleteWordstatReport';
$this->request['param'] = (int)$id;
$this->request['login'] = $this->login;
$response = $this->request();
return $response->data;
}
/**
* Remove from string all invalid characters
* Leave only English, Russian, Turkish, Kazakh and Numbers
* @link http://www.unicode.org/charts/
* @param string $w
* @return string
*/
public static function prepareWord($w){
return YADWord::prepare($w);
}
public function delReport($id){
return $this->deleteReport($id);
}
public function removeReport($id){
return $this->deleteReport($id);
}
public function createReport(array $keywords = array(), array $regions = array()){
foreach($keywords as $k=>$v)
$keywords[$k] = self::magicUTF8($v);
if($keywords && count($keywords)){
$this->request['method'] = 'CreateNewWordstatReport';
$this->request['param'] = array(
'Phrases' => $keywords,
'GeoID' => $regions
);
$this->request['login'] = $this->login;
$response = $this->request();
if(isset($response->error_code)){
$error_code = (int)$response->error_code;
var_dump($response);
return (0 - $error_code);
}
if(!isset($response->data)) return false;
return $response->data;
}
return false;
}
private function request(){
$request = json_encode($this->request);
# параметры запроса
$opts = array(
'http'=>array(
'method' => 'POST',
'content' => $request,
'header' => 'Content-type: application/json; charset=utf-8' . "\r\n"
)
);
# создание контекста потока
$context = stream_context_create($opts);
# подключаем объединенный с приватным ключом сертификат
stream_context_set_option($context, 'ssl', 'local_cert', $this->certdir . '/solid-cert.crt');
# отправляем запрос и получаем ответ от сервера
$result = file_get_contents(self::JSON_URL, 0, $context);
return json_decode($result);
}
public static function magicUTF8($s){
$s = iconv('utf-8', 'windows-1251', $s);
$s = iconv('windows-1251', 'utf-8', $s);
$s = iconv('ISO-8859-1', 'utf-8', $s);
return $s;
}
public static function crc($keyword, $region, $strict = 0){
$strict = (int)$strict;
return crc32('yafreq_'.str_replace(' ','-',mb_strtolower($keyword,'UTF-8')).'_'.$region.'_'.$strict);
}
}