-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyworddesk-customer-api.php
201 lines (173 loc) · 6.44 KB
/
keyworddesk-customer-api.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
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
<?php
/**
PHP Keyworddesk API Class for simple calling via functions
**/
include 'keyword-filter.php';
include 'keyword-request.php';
class KeyworddeskApi {
private $token;
// the url for the api login and to the api base,including the version tag for the api version you want to use
private $urlApiLogin = 'http://api.keyworddesk.com/api/login';
private $urlApiBase = 'http://api.keyworddesk.com/v1';
// possible fields for getKeywordData api call
private $possibleFields = array(
'suggestedBid',
'googleInTitleCount',
'googleResultCount',
'searchVolume',
'searchVolumeJanuary',
'searchVolumeFebruary',
'searchVolumeMarch',
'searchVolumeApril',
'searchVolumeMay',
'searchVolumeJune',
'searchVolumeJuly',
'searchVolumeAugust',
'searchVolumeSeptember',
'searchVolumeOctober',
'searchVolumeNovember',
'searchVolumeDecember',
);
// possible COUNT TYPES
public static $COUNT_TYPE_ALL = 0;
public static $COUNT_TYPE_SHORTTAIL = 1;
public static $COUNT_TYPE_LONGTAIL = 2;
public static $COUNT_TYPE_HAVE_PLANNER_DATA = 3;
public static $FILTER_ILIKE = "ILike";
public static $FILTER_NOTILIKE = "NotILike";
public static $FILTER_LIKE = "Like";
public static $FILTER_NOTLIKE = "NotLike";
public static $FILTER_EQUAL = "Equal";
public static $FILTER_NOTEQUAL = "NotEqual";
public static $FILTER_IBEGINSWITH = "IBeginsWith";
public static $FILTER_BEGINSWITH = "BeginsWith";
public static $FILTER_IENDSWITH = "IEndsWith";
public static $FILTER_ENDSWITH = "EndsWith";
public static $FILTER_NUMERIC_EQUAL = "Equal";
public static $FILTER_NUMERIC_NOT_EQUAL = "NotEqual";
public static $FILTER_NUMERIC_LESS_THAN = "LessThan";
public static $FILTER_NUMERIC_LESS_THAN_EQUALS = "LessThanEquals";
public static $FILTER_NUMERIC_GREATER_THAN = "GreaterThan";
public static $FILTER_NUMERIC_GREATER_THAN_EQUALS = "GreaterThanEquals";
public static $FILTER_NUMERIC_BETWEEN = "Between";
public static $FILTER_NUMERIC_IS_NULL = "IsNull";
public static $FILTER_NUMERIC_IS_NOT_NULL = "IsNotNull";
public static $FIELD_SEARCHVOLUME = "searchVolume";
public static $FIELD_SUGGESTEDBID = "suggestedBid"; // also known as CPC
public static $FIELD_COMPETITION = "competition";
public static $FIELD_GOOGLE_RESULT_COUNT = "googleResultCount";
public static $FIELD_GOOGLE_INTITLE_COUNT = "googleInTitleCount";
public static $FIELD_SEARCHVOLUME_JANUARY = "searchVolumeJanuary";
public static $FIELD_SEARCHVOLUME_FEBRUARY = "searchVolumeFebruary";
public static $FIELD_SEARCHVOLUME_MARCH = "searchVolumeMarch";
public static $FIELD_SEARCHVOLUME_APRIL = "searchVolumeApril";
public static $FIELD_SEARCHVOLUME_MAY = "searchVolumeMay";
public static $FIELD_SEARCHVOLUME_JUNE = "searchVolumeJune";
public static $FIELD_SEARCHVOLUME_JULY = "searchVolumeJuly";
public static $FIELD_SEARCHVOLUME_AUGUST = "searchVolumeAugust";
public static $FIELD_SEARCHVOLUME_SEPTEMBER = "searchVolumeSeptember";
public static $FIELD_SEARCHVOLUME_OCTOBER = "searchVolumeOctober";
public static $FIELD_SEARCHVOLUME_NOVEMBER = "searchVolumeNovember";
public static $FIELD_SEARCHVOLUME_DECEMBER = "searchVolumeDecember";
public function __construct($username="", $password="") {
if($username!=""&&$password!=""){
$this->login($username, $password);
}
}
// perform the login and stores the token in object
private function login($username,$password) {
$loginArray = array('username' => $username, 'password' => $password);
$login = $this->makeCall($this->getLoginUrl(),json_encode($loginArray));
$this->setToken($login->token);
}
// gets the keyworddata via makeCall submits keywords and fields if set
public function getKeywordData($keywords,$fields = null) {
$data = array();
foreach($keywords AS $keyword){
$requestArray = array();
$requestArray["keyword"] = $keyword;
if($fields != null){
foreach($fields AS $field) {
if(in_array($field,$this->possibleFields)) {
$requestArray[$field] = true;
}
}
} else {
$requestArray["searchVolume"] = true;
}
$data[] = $requestArray;
}
return $this->makeCall($this->getBaseUrl().'/getKeywordData',json_encode($data));
}
// gets the keywordcount via makeCall
public function getKeywordCount($countType) {
$requestArray = array();
$requestArray["countType"] = $countType;
return $this->makeCall($this->getBaseUrl().'/getKeywordCount',json_encode($requestArray))->count;
}
private function makeCall($url, $content) {
$postArray = null;
if($url === $this->getLoginUrl()) {
$postArray = array (
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json'."\r\n",
'content' => $content
)
);
} else {
$postArray = array (
'http' => array(
'method' => 'POST',
'header' => 'X-Auth-Token: '.$this->getToken()."\r\n".
'Content-type: application/json'."\r\n",
'content' => $content
)
);
}
$context = stream_context_create($postArray);
return json_decode(file_get_contents($url,false,$context));
}
public function filterKeywords($filter) {
return $this->filter($filter)->keywords;
}
public function filter($filter) {
$jsonObject = $filter->toJson();
$keywordResultList = $this->makeCall($this->getBaseUrl().'/filterKeywords',json_encode($jsonObject));
return $keywordResultList;
}
public function getCreditBalance() {
$responseContent = $this->makeCall($this->getBaseUrl().'/getCreditBalance',"");
return $responseContent;
}
public function hasCredits() {
if($this->getCreditBalance()->creditsLeft > 0) {
return true;
}
return false;
}
public function orderKeywordData($keywordRequest){
if(!is_array($keywordRequest)){
$keywordRequest = array($keywordRequest);
}
$jsonString = json_encode($keywordRequest);
$response = $this->makeCall($this->getBaseUrl().'/orderKeywordData',$jsonString);
return json_decode($response)->jobCreated->id;
}
public function logout(){
$this->makeCall($this->getBaseUrl().'/logout',"");
}
public function getToken() {
return $this->token;
}
public function setToken($token) {
$this->token = $token;
}
private function getLoginUrl() {
return $this->urlApiLogin;
}
private function getBaseUrl() {
return $this->urlApiBase;
}
}
?>