-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckFace.php
119 lines (101 loc) · 2.94 KB
/
CheckFace.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
<?php
/**
* Created by PhpStorm.
* User: ikosar
* Date: 19/04/2018
* Time: 04:32 PM
*/
namespace ikosar\LMFA;
use HTTP_Request2;
use HttpException;
class CheckFace
{
// This Var must add to config file.
public function getApiKey()
{
$this->api_key = config('lmfa.api_key');
return $this->api_key;
}
public function setUrl($url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect")
{
$this->url = $url;
}
public function setImage(array $image)
{
$this->image = $image;
}
public function setHeaders(array $headers)
{
$this->headers = $headers;
}
public function setParameters(array $parameters)
{
$this->parameters = $parameters;
}
public function setMethod($method = 'post')
{
$this->method = $method;
}
public function setBody(array $body, $is_simple_request = true)
{
$this->body = $body;
$this->request_type = $is_simple_request;
}
public function check()
{
if (isset($this->url) AND isset($this->headers) AND isset($this->parameters) AND isset($this->method) AND isset($this->body)) {
$this->check = true;
return true;
}
$this->check = false;
return false;
}
public function send()
{
if ($this->check) {
if ($this->url)
{
$request = new Http_Request2($this->url);
}
else {
$request = new Http_Request2('https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect');
}
$url = $request->getUrl();
$headers = $this->headers;
$request->setHeader($headers);
$parameters = $this->parameters;
$url->setQueryVariables($parameters);
switch ($this->method) {
case "post":
$mtd = HTTP_Request2::METHOD_POST;
break;
case "get":
$mtd = HTTP_Request2::METHOD_GET;
break;
case "delete":
$mtd = HTTP_Request2::METHOD_DELETE;
break;
case "put":
$mtd = HTTP_Request2::METHOD_PUT;
break;
default:
return "ERROR METHOD GIVEN WAS NOT VALID,ONLY LOWERCASE letter must used. ";
break;
};
$request->setMethod($mtd);
if ($this->request_type == true) {
$simple = json_encode($this->image);
$request->setBody($simple);
} else {
$request->setBody($this->body);
}
try {
$response = $request->send();
return $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
}
return null;
}
}