-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoRankerAPI.php
More file actions
209 lines (190 loc) · 9.14 KB
/
Copy pathGeoRankerAPI.php
File metadata and controls
209 lines (190 loc) · 9.14 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Class GeoRanker API.
* Conect with GeoRanker Api (apidocs.georanker.com)
* @author Renan Gomes
*/
if (!class_exists("GeoRankerAPI")) {
class GeoRankerAPI {
private $email = null;
private $apikey = null;
private $session = null;
private $apiurl = "https://api.georanker.com/v2";
private $cachefolder = null;
private $cache = true;
private $cachetime = 60;
/**
* Set the conection mode for the GeoRanker API interaction
* @param string $conectionmode can be 'curl' or 'fsockopen'
*/
public function GeoRankerAPI($email, $apikey, $cache = true, $cachetime = 60, $cachefolder = null) {
$this->email = $email;
$this->apikey = strtolower($apikey);
if (!empty($cachefolder)) {
$this->cachefolder = $cachefolder;
} else {
$this->cachefolder = sys_get_temp_dir() . "/";
//$this->cachefolder = dirname(__FILE__) . "/cache/";
}
$this->cachetime = $cachetime;
$this->cache = $cache;
}
public function login() {
if (empty($this->email) || empty($this->apiurl)) {
return false;
}
$ret = $this->docurl($this->apiurl . '/api/login.json?' . http_build_query(array('email' => $this->email, 'apikey' => $this->apikey)));
if (!empty($ret['content'])) {
$responseobj = json_decode(trim($ret['content']));
if (!empty($responseobj) && !isset($responseobj->debug)) {
$this->session = $responseobj->session;
}
return $responseobj;
}
return false;
}
public function accountinfo() {
if (empty($this->session) && !$this->login()) {
return false;
}
$ret = $this->docurl($this->apiurl . '/account/info.json?' . http_build_query(array('email' => $this->email, 'session' => $this->session)));
if (!empty($ret['content'])) {
$responseobj = json_decode(trim($ret['content']));
return $responseobj;
}
return false;
}
public function reportnew($type, $keywords, $countries, $is_global, $maxcities, $regions = array(), $url = NULL, $language = NULL, $ignoretypes = null, $is_usealternativetld = FALSE, $is_fillcities = TRUE, $is_formobile = FALSE, $callback = '', $brand = NULL, $is_gmsearchmode = FALSE, $is_localonly = FALSE, $is_carouselfallbackmode = FALSE, $phone = NULL, $fields = NULL) {
if (empty($this->session) && !$this->login()) {
return false;
}
$post_fields = array();
$post_fields['type'] = strtolower(trim($type));
$post_fields['keywords'] = (empty($keywords)) ? array() : $keywords;
$post_fields['countries'] = (empty($countries)) ? array() : $countries;
$post_fields['regions'] = (empty($regions)) ? array() : $regions;
$post_fields['url'] = (empty($url)) ? NULL : trim($url);
$post_fields['language'] = empty($language) ? NULL : trim($language);
$post_fields['ignoretypes'] = (empty($ignoretypes)) ? '' : $ignoretypes;
$post_fields['is_usealternativetld'] = (empty($is_usealternativetld)) ? FALSE : TRUE;
$post_fields['is_global'] = (empty($is_global)) ? FALSE : TRUE;
$post_fields['is_fillcities'] = (empty($is_fillcities)) ? FALSE : TRUE;
$post_fields['is_formobile'] = (empty($is_formobile)) ? FALSE : TRUE;
$post_fields['maxcities'] = $maxcities;
$post_fields['callback'] = (empty($callback)) ? NULL : $callback;
$post_fields['brand'] = (empty($brand)) ? NULL : $brand;
$post_fields['is_gmsearchmode'] = (empty($is_gmsearchmode)) ? FALSE : TRUE;
$post_fields['is_localonly'] = (empty($is_localonly)) ? FALSE : TRUE;
$post_fields['is_carouselfallbackmode'] = (empty($is_carouselfallbackmode)) ? FALSE : TRUE;
$post_fields['fields'] = (empty($fields)) ? NULL : $fields;
$post_fields['phone'] = (empty($phone)) ? NULL : $phone;
$ret = $this->docurl($this->apiurl . '/report/new.json?' . http_build_query(array('email' => $this->email, 'session' => $this->session)), 'POST', 30, array(), json_encode((object) $post_fields));
//return var_dump($ret);
if (!empty($ret['content'])) {
$responseobj = json_decode(trim($ret['content']));
return $responseobj;
}
return false;
}
public function countrylist() {
if (empty($this->session) && !$this->login()) {
return false;
}
$ret = $this->docurl($this->apiurl . '/country/list.json?' . http_build_query(array('email' => $this->email, 'session' => $this->session)));
if (!empty($ret['content'])) {
$responseobj = json_decode(trim($ret['content']));
return $responseobj;
}
return false;
}
public function reportget($id, $subtype = '') {
if (empty($id) || (empty($this->session) && !$this->login())) {
return false;
}
if ($subtype === '1stpage') {
$subtype = 'firstpage';
}
if ($subtype === 'advertisers') {
$subtype = 'advertisers';
}
$ret = $this->docurl($this->apiurl . '/report/' . (!empty($subtype) ? $subtype . '/' : '') . ((int) $id) . '.json?' . http_build_query(array('email' => $this->email, 'session' => $this->session)));
if (!empty($ret['content'])) {
$responseobj = json_decode(trim($ret['content']));
return $responseobj;
}
return false;
}
/**
* Make a json string be easier to read
* @param string $json a valid json string
* @return string The new and improved json string
*/
static function beautifyJSON($json) {
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = ' ';
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
for ($i = 0; $i <= $strLen; $i++) {
// Grab the next character in the string.
$char = substr($json, $i, 1);
// Are we inside a quoted string?
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
// If this character is the end of an element,
// output a new line and indent the next line.
} else if (($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos--;
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
// Add the character to the result string.
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line.
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
return $result;
}
private function docurl($url, $method = 'GET', $timeout = 30, $options = array(), $post_fields = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-length: ' . strlen($post_fields)));
}
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$content = substr($response, $header_size);
$theinfo = curl_getinfo($ch);
//var_dump($theinfo);
curl_close($ch);
$outarray = array('headers' => $headers, 'content' => $content, 'info' => $theinfo);
return $outarray;
}
}
}