Repository navigation
Expand file tree
/
Copy pathProRankTracker.php
More file actions
93 lines (81 loc) · 3.19 KB
/
Copy pathProRankTracker.php
File metadata and controls
93 lines (81 loc) · 3.19 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
<?php
namespace LunarDevelopment\ProRankTracker;
/**
* Class ProRankTracker
* @package LunarDevelopment\ProRankTracker
*/
class ProRankTracker {
private $_email;
private $_password;
private $_url;
/**
* ProRankTracker constructor.
* @param $email
* @param $password
* @param array $params
*/
function __construct($email, $password, $params=array()) {
$this->_email = $email;
$this->_password = $password;
$this->_url = 'https://proranktracker.com/api/';
}
/**
* @param $cmd
* @param array $args
* @return mixed|object
*/
function call($cmd, $args=array()) {
$args['cmd'] = $cmd;
return $this->__x_call($args);
}
private function __x_call($args) {
$ch = curl_init($this->_url);
curl_setopt($ch, CURLOPT_USERPWD, $this->_email . ":" . $this->_password);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->__http_build_multi_query($args));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, "ProRankTracker API PHP-Client");
// curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
if (curl_errno($ch) != CURLE_OK) {
return (object) array(
'result' => 'error',
'error_message' => "Failed to call remote server: " . curl_error($ch)
);
} else {
$info = curl_getinfo($ch);
if ($info['http_code'] !== 200) {
return (object) array(
'result' => 'error',
'error_message' => "Remote Server Error: " . $info['http_code']
);
}
curl_close($ch);
return json_decode($response);
}
}
public function __call($function, $args) {
$function = str_replace('_', '.', $function);
if ($function === 'urls.look.up.by.url') $function = 'urls.look_up_by_url';
if (! in_array($function, array('urls.get', 'urls.look_up_by_url', 'urls.new', 'url.get', 'url.delete', 'url.edit', 'terms.get', 'terms.new', 'term.get', 'term.delete', 'term.enable', 'term.disable', 'term.run', 'se.get', 'groups.get', 'groups.new', 'group.edit', 'group.get', 'group.delete', 'url.group.add', 'url.group.delete', 'tags.get', 'tags.new', 'tag.get', 'tag.delete', 'term.tag.add', 'term.tag.delete'))) {
return (object) array(
'result' => 'error',
'error_message' => 'Unknown command'
);
}
if (is_array($args) && count($args) == 1 && is_array($args[0])) $args = $args[0]; // Hack
return $this->call($function, $args);
}
private function __http_build_multi_query($data, $key=NULL) {
$query = array();
foreach ($data as $k => $value) {
if (is_array($value)) {
$query[] = $this->__http_build_multi_query($value, $k . '[]');
} else {
$query[] = urlencode(is_null($key) ? $k : $key) . '=' . rawurlencode($value);
}
}
return implode('&', $query);
}
}