This repository was archived by the owner on Jun 7, 2024. It is now read-only.
forked from Mushmou/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
52 lines (43 loc) · 1.56 KB
/
index.php
File metadata and controls
52 lines (43 loc) · 1.56 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
<?php
return function ($req, $res) {
// Input validation
$url = null;
try {
$payload = \json_decode($req['payload'], true);
$url = \trim($payload['url']);
} catch(\Exception $err) {
\var_dump($err);
throw new \Exception('Payload is invalid.');
}
if(empty($url)) {
throw new \Exception('Invalid url.');
}
// Make sure we have envirnment variables required to execute
if(
empty($req['variables']['CLOUDMERSIVE_API_KEY'])
) {
throw new \Exception('Please provide all required environment variables.');
}
// Download image to buffer
$ch = \curl_init($url);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Mimick a Chrome User-Agent incase the URL is behind a proxy
\curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2');
$image = \curl_exec($ch);
\curl_close($ch);
// Upload to cloudmersive
$ch = \curl_init('https://api.cloudmersive.com/image/recognize/detect-objects');
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, [
'imageFile' => $image,
]);
\curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Apikey: ' . $req['variables']['CLOUDMERSIVE_API_KEY'],
]);
$response = \json_decode(\curl_exec($ch), true);
\curl_close($ch);
// Return phone number prefix
$res->json($response['Objects']);
};