1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: ikosar
5+ * Date: 19/04/2018
6+ * Time: 04:32 PM
7+ */
8+
9+ namespace ikosar \LMFA ;
10+
11+
12+
13+ use HTTP_Request2 ;
14+ use HttpException ;
15+
16+ class CheckFace
17+ {
18+ // This Var must add to config file.
19+
20+
21+ public function getApiKey ()
22+ {
23+ $ this ->api_key = config ('lmfa.api_key ' );
24+ return $ this ->api_key ;
25+ }
26+
27+ public function setUrl (array $ url )
28+ {
29+ $ this ->url = $ url ;
30+ }
31+
32+ public function setHeaders (array $ headers )
33+ {
34+ $ this ->headers = $ headers ;
35+ }
36+
37+ public function setParameters (array $ parameters )
38+ {
39+ $ this ->parameters = $ parameters ;
40+ }
41+
42+ public function setMethod ($ method = 'post ' )
43+ {
44+ $ this ->method = $ method ;
45+ }
46+
47+ public function setBody ($ body = true , $ is_simple_request = true )
48+ {
49+ $ this ->body = $ body ;
50+ $ this ->request_type = $ is_simple_request ;
51+ }
52+
53+ public function check ()
54+ {
55+ if (isset ($ this ->url ) AND isset ($ this ->headers ) AND isset ($ this ->parameters ) AND isset ($ this ->method ) AND isset ($ this ->body )) {
56+ $ this ->check = true ;
57+ return true ;
58+ }
59+
60+ $ this ->check = false ;
61+ return false ;
62+ }
63+
64+ public function send ()
65+ {
66+ if ($ this ->check ) {
67+ if ($ this ->request_type == true )
68+ {
69+ $ request = new Http_Request2 ('https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect ' );
70+ }
71+ else
72+ {
73+ $ request = new Http_Request2 ($ this ->url );
74+ }
75+ $ url = $ request ->getUrl ();
76+ $ headers = $ this ->headers ;
77+ $ request ->setHeader ($ headers );
78+ $ parameters = $ this ->parameters ;
79+ $ url ->setQueryVariables ($ parameters );
80+ switch ($ this ->method ) {
81+ case "post " :
82+ $ mtd = HTTP_Request2::METHOD_POST ;
83+ break ;
84+ case "get " :
85+ $ mtd = HTTP_Request2::METHOD_GET ;
86+ break ;
87+ case "delete " :
88+ $ mtd = HTTP_Request2::METHOD_DELETE ;
89+ break ;
90+ case "put " :
91+ $ mtd = HTTP_Request2::METHOD_PUT ;
92+ break ;
93+ default :
94+ return "ERROR METHOD GIVEN WAS NOT VALID,ONLY LOWERCASE letter must used. " ;
95+ break ;
96+ };
97+ $ request ->setMethod ($ mtd );
98+ if ($ this ->request_type == true ) {
99+ $ simple = json_encode ($ this ->url );
100+ $ request ->setBody ($ simple );
101+ } else {
102+ $ request ->setBody ($ this ->body );
103+ }
104+
105+ try {
106+ $ response = $ request ->send ();
107+ return $ response ->getBody ();
108+ } catch (HttpException $ ex ) {
109+ echo $ ex ;
110+ }
111+
112+ }
113+ return null ;
114+ }
115+
116+ }
0 commit comments