This repository was archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeutrinoAPI.php
235 lines (205 loc) · 7.16 KB
/
NeutrinoAPI.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* A PHP client for Neutrino API, see: https://www.neutrinoapi.com
* This class currently does not cover ALL of the APIs (but feel free to add more if you find this useful!)
*/
class NeutrinoAPI
{
// set your actual user-id here
private $apiUser = 'test_user_1578';
// set your actual api-key here
private $apiKey = 'W6du2QO2wXItdjdvQh9W8oEvssfZZKQN';
const API_BASE_URL = 'https://neutrinoapi.com/';
const DEFAULT_REQUEST_TIMEOUT = 20; // default API request timeout in seconds
/**
* Get gelocation information about an IP address
* See: http://www.neutrinoapi.com/api/ip-info/
* @param string $ipAddress
* @param boolean $reverseLookup
* @return array
*/
public function ipInfo($ipAddress, $reverseLookup = false)
{
$data = array();
$data['ip'] = $ipAddress;
if ($reverseLookup) $data['reverse-lookup'] = 'true';
return $this->postRequest("ip-info", $data);
}
/**
* Validate and clean an email address
* See: http://www.neutrinoapi.com/api/email-validate/
* @param string $emailAddress
* @param boolean $fixTypos
* @return array
*/
public function emailValidate($emailAddress, $fixTypos = false)
{
$data = array();
$data['email'] = $emailAddress;
if ($fixTypos) $data['fix-typos'] = 'true';
return $this->postRequest("email-validate", $data);
}
/**
* Validate, format and get location information about a phone number
* See: http://www.neutrinoapi.com/api/phone-validate/
* @param string $phoneNumber
* @param string $countryCode
* @return array
*/
public function phoneValidate($phoneNumber, $countryCode = '')
{
$data = array();
$data['number'] = $phoneNumber;
if (!empty($countryCode)) $data['country-code'] = $countryCode;
return $this->postRequest("phone-validate", $data);
}
/**
* Convert currency and most known measurement types
* See: http://www.neutrinoapi.com/api/convert/
* @param any $fromValue
* @param string $fromType
* @param string $toType
* @return array
*/
public function convert($fromValue, $fromType, $toType)
{
$data = array();
$data['from-value'] = $fromValue;
$data['from-type'] = $fromType;
$data['to-type'] = $toType;
return $this->postRequest("convert", $data);
}
/**
* Get detailed user-agent and mobile device information from a user-agent string
* See: http://www.neutrinoapi.com/api/user-agent-info/
* @param string $userAgent
* @return array
*/
public function userAgentInfo($userAgent)
{
$data = array();
$data['user-agent'] = $userAgent;
return $this->postRequest("user-agent-info", $data);
}
/**
* Clean and sanitize untrusted HTML
* See: http://www.neutrinoapi.com/api/html-clean/
* @param string $content
* @param string $outputType
* @return string
*/
public function htmlClean($content, $outputType)
{
$data = array();
$data['content'] = $content;
$data['output-type'] = $outputType;
return $this->postRequest("html-clean", $data, false);
}
/**
* Extract HTML tag contents or attributes from HTML
* See: http://www.neutrinoapi.com/api/html-extract-tags/
* @param string $content
* @param string $tag
* @param string $attribute
* @param string $baseURL
* @return array
*/
public function htmlExtract($content, $tag, $attribute = '', $baseURL = '')
{
$data = array();
$data['content'] = $content;
$data['tag'] = $tag;
$data['attribute'] = $attribute;
$data['base-url'] = $baseURL;
return $this->postRequest("html-extract-tags", $data);
}
/**
* Detect and censor bad words and swear words
* See: http://www.neutrinoapi.com/api/bad-word-filter/
* @param string $content
* @param string $censorCharacter
* @return array
*/
public function badWordFilter($content, $censorCharacter = '*')
{
$data = array();
$data['content'] = $content;
$data['censor-character'] = $censorCharacter;
return $this->postRequest("bad-word-filter", $data);
}
/**
* Convert HTML to a PDF document
* See: http://www.neutrinoapi.com/api/html-to-pdf/
* @param string $content
* @param string $filePath the file path to save the PDF to
* @param number $htmlWidth
* @param number $margin
* @param string $title
*/
public function htmlToPDF($content, $filePath, $htmlWidth = '', $margin = '', $title = '')
{
$data = array();
$data['content'] = $content;
$data['html-width'] = $htmlWidth;
$data['margin'] = $margin;
$data['title'] = $title;
$this->postRequestToFile("html-to-pdf", $data, $filePath, 25);
}
/**
* Generate a QR code image (PNG)
* See: http://www.neutrinoapi.com/api/qr-code/
* @param string $content
* @param string $filePath the file path to save the PNG image to
* @param number $width
* @param number $height
* @param string $fgColor
* @param string $bgColor
*/
public function qrCode($content, $filePath, $width = '', $height = '', $fgColor = '', $bgColor = '')
{
$data = array();
$data['content'] = $content;
$data['width'] = $width;
$data['height'] = $height;
$data['fg-color'] = $fgColor;
$data['bg-color'] = $bgColor;
$this->postRequestToFile("qr-code", $data, $filePath);
}
/**
* Post to the API and return response as either associative array or as-is
*/
private function postRequest($path, $data, $returnArray = true, $timeout = self::DEFAULT_REQUEST_TIMEOUT)
{
$data['user-id'] = $this->apiUser;
$data['api-key'] = $this->apiKey;
$ch = curl_init(self::API_BASE_URL.$path);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
if ($returnArray) return json_decode($content, true);
else return $content;
}
/**
* Post to the API and save the response into a file
*/
private function postRequestToFile($path, $data, $filePath, $timeout = self::DEFAULT_REQUEST_TIMEOUT)
{
$out = fopen($filePath, "wb");
if ($out === FALSE) return false;
$data['user-id'] = $this->apiUser;
$data['api-key'] = $this->apiKey;
$ch = curl_init(self::API_BASE_URL.$path);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_exec($ch);
curl_close($ch);
return true;
}
}
?>