This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuedaRequest.php
127 lines (103 loc) · 3.64 KB
/
BuedaRequest.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
<?php
/**
* BuedaRequest class encapsulates the functionality required to make a request
* to Bueda Web Service.
*
* @author Vignesh
*/
class BuedaRequest {
// <string> : Domain of bueda request url
private $domain = "http://api.bueda.com/enriched?";
// <string> : API key of the user
private $api_key;
// <string> : comma-separated string of the tags
private $tags;
// <resource> : CURL resource handler
private $curl_resource;
/**
* Initializes a BuedaRequest Object with the specified api key and tags
*
* @param <string> $api_key : your bueda api key
* @param <array> $parameters : comma-separated string of tags
*/
function __construct($api_key,$tags=NULL) {
$this->api_key = $api_key;
if(isset($tags)) {
// If the $tags parameter is a string, assign it to the $tags instance variable
if(is_string($tags)) {
$this->tags = $tags;
}
else{
throw new InvalidTagException("Invalid tags supplied. The supplied tag(s):".$tags);
}
}
$this->curl_resource = curl_init();
curl_setopt($this->curl_resource, CURLOPT_RETURNTRANSFER, 1);
}
/**
* Adds a tag or comma-separated string of tags to the existing tag list
*
* @param <mixed> $tag : new tag(or string of tags) to be added to the existing tag list
*/
function addTags($tags) {
if(!isset($this->tags)){
$this->reloadTags($tags);
return ;
}
// If the $tags parameter is a string, assign it to the $tags instance variable
if(is_string($tags)) {
$this->tags .= ",".$tags;
}
else{
throw new InvalidTagException("Invalid tags supplied. The supplied tag(s):".$tags);
}
}
/**
* Reloads the existing tag list with the given tag or comma-separated string of tags. Removes
* the already available tags in the current object
*
* @param <mixed> $tags : new tag(or comma-separated string of tags)
*/
function reloadTags($tags) {
// If the $tags parameter is a string, assign it to the $tags instance variable
if(is_string($tags)) {
$this->tags = $tags;
}
else{
throw new InvalidTagException("Invalid tags supplied. The supplied tag(s):".$tags);
}
}
/**
* Executes the query and returns the json object as an associative array.
* Throws an error if tags are not already supplied to the current object.
*
* @return <array> : associative array representing the json object
*/
function execute() {
if(!$this->tags) {
throw new TagNotSuppliedException("Tags not available to complete the request");
}
// Contruct the URL with the necessary details
$url = $this->domain."apikey=".$this->api_key."&tags=".$this->tags;
// Set the URL, execute and obtain the response as json text string
curl_setopt($this->curl_resource, CURLOPT_URL, $url);
$response = curl_exec($this->curl_resource);
// Parse the json text string to product json object represented as an associate array
$json = json_decode($response,true);
return $json;
}
function __destruct() {
curl_close($this->curl_resource);
}
/**
* Used for testing only
* @return <type>
*/
private function getURLString() {
return $this->domain."apikey=".$this->api_key."&tags=".$this->tags;
}
function __toString(){
return $this->getURLString();
}
}
?>