-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrovitApi.php
More file actions
152 lines (129 loc) · 3.77 KB
/
TrovitApi.php
File metadata and controls
152 lines (129 loc) · 3.77 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
<?php
namespace DawnAngel\TrovitApi;
use \Exception;
/**
* Trovit Api Class
*
* @author Eric Pinto <ericpinto1985@gmail.com>
*/
class TrovitApi
{
/**
* Api URI value
* @var string
*/
protected static $API_URI = 'https://api.trovit.com/v2/{vertical}/{resource}';
/**
* Api token value
* @var string
*/
protected static $TOKEN;
/**
* Request options cache
* @var array
*/
protected static $REQUEST_OPTIONS_CACHE;
const RESOURCE_ADS = 'ads';
const VERTICAL_HOMES = 'homes';
const VERTICAL_CARS = 'cars';
const VERTICAL_JOBS = 'jobs';
/**
* Setter for Api token
*
* @param string $token The new Api token
*/
public static function setToken($token)
{
self::$TOKEN = $token;
/** Reset the REQUEST OPTIONS when new apikey is setted */
if (isset(self::$REQUEST_OPTIONS_CACHE)) {
self::$REQUEST_OPTIONS_CACHE = null;
}
}
/**
* Setter for Api URI
* (This could be used for testing purposes)
*
* @param string $apiUri The new Api URI
*/
public static function setApiUri($apiUri)
{
self::$API_URI = $apiUri;
}
/**
* Configure the inner request options
* (Lazy loaded)
*
* @return array Request options
*/
protected static function getRequestOptions()
{
if (!isset(self::$REQUEST_OPTIONS_CACHE)) {
self::$REQUEST_OPTIONS_CACHE = array(
'http' => array(
'method' => 'GET',
'header' => "X-Client-ID: " . self::$TOKEN . "\r\n",
)
);
}
return self::$REQUEST_OPTIONS_CACHE;
}
/**
* Handle all the data collection
*
* @param string $url Request url
*
* @return string
*/
protected static function getUrlData($url)
{
$context = stream_context_create(self::getRequestOptions());
return @file_get_contents($url, false, $context);
}
/**
* Do the request against the ApiUri and return the Api response
*
* @param string $vertical Vertical from the class constants
* @param array $params Request params
* @param string $resource Resource from the class constants
* @param boolean $debug Send debug information to the standard output
*
* @return string Response as JSON string
*/
public static function doRequestJson($vertical, $params, $resource = self::RESOURCE_ADS, $debug = false)
{
$requestUrl = str_replace(
array('{vertical}', '{resource}'),
array($vertical, $resource),
self::$API_URI
);
$requestUrl .= '?' . http_build_query($params);
if ($debug) {
printf("API url: %s\n", $requestUrl);
}
try {
$result = self::getUrlData($requestUrl);
} catch (Exception $e) {
$result = false;
}
if ($debug) {
printf("Request status: %s\n", $result === false ? 'Fail' : 'OK!');
}
return $result;
}
/**
* Do the request against the ApiUri and return the Api response
*
* @param string $vertical Vertical from the class constants
* @param array $params Request params
* @param string $resource Resource from the class constants
* @param boolean $debug Send debug information to the standard output
*
* @return array Response
*/
public static function doRequest($vertical, $params, $resource = self::RESOURCE_ADS, $debug = false)
{
$responseJson = self::doRequestJson($vertical, $params, $resource, $debug);
return ($responseJson !== false) ? json_decode($responseJson, true) : false;
}
}