-
Notifications
You must be signed in to change notification settings - Fork 0
/
bighugelabs.class.php
110 lines (106 loc) · 3.75 KB
/
bighugelabs.class.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
<?php
class BigHugeLabs{
private $filename;
private $base_url = 'http://words.bighugelabs.com/api/2';
private $api_key = '????????';
function __construct(){
}
private function generateFileName(){
$this->filename = 'synonyms_'. time() .'.csv';
return $this->filename;
}
private function writeToFile($keyword, $words){
if (file_exists('tmp\\' . $this->filename))
$fh = fopen('tmp\\' .$this->filename, 'a');
else
$fh = fopen('tmp\\' .$this->filename, 'w');
fputcsv($fh, $words);
fclose($fh);
}
private function getSynonyms($keyword, $format = 'php'){
$url = $this->base_url.'/'.$this->api_key.'/'.$keyword.'/'.$format;
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
private function getAntonyms($keyword, $format = 'php'){
$url = $this->base_url.'/'.$this->api_key.'/'.$keyword.'/'.$format;
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
public function bighugelabs_search($search_for, $search_in) {
foreach ($search_in as $key => $element) {
if ( ($key === $search_for) || (is_array($element) && $this->bighugelabs_search($search_for, $element)) ){
echo array_shift(array_values($element));
exit;
}
}
return NULL;
exit;
}
public function getKeywordSynonyms($keyword){
$keyword = trim($keyword);
if(empty($keyword))
return false;
try {
$output = $this->getAntonyms($keyword);
$output = $this->bighugelabs_search("syn", unserialize($output));
/*IF any input miss the data from target, again make a call*/
if(empty($output)){
$output = $this->getAntonyms($keyword);
///$output = json_decode($output,true);
}
return $output;
} catch (Exception $e) {
//header('Content-Type: application/json');
return array('status' => false, 'error_message' => $e->getMessage());
}
}
public function getKeywordAntonyms($keyword){
$keyword = trim($keyword);
if(empty($keyword))
return false;
try {
$output = $this->getAntonyms($keyword);
$output = $this->bighugelabs_search("ant", unserialize($output));
/*IF any input miss the data from target, again make a call*/
if(empty($output)){
$output = $this->getAntonyms($keyword);
///$output = json_decode($output,true);
}
return $output;
} catch (Exception $e) {
//header('Content-Type: application/json');
return array('status' => false, 'error_message' => $e->getMessage());
}
}
public function getKeywordArray($keyword){
$keyword = trim($keyword);
if(empty($keyword))
return false;
try {
$output = $this->getAntonyms($keyword);
$output = unserialize($output);
/*IF any input miss the data from target, again make a call*/
if(empty($output)){
$output = $this->getAntonyms($keyword);
///$output = json_decode($output,true);
}
return $output;
} catch (Exception $e) {
//header('Content-Type: application/json');
return array('status' => false, 'error_message' => $e->getMessage());
}
}
public function quit(){
exit;
}
}
?>