Vous êtes connecté en tant que anonymous Se Deconnecter
Browse code

mise en place des actions de vue, du loggeur du MVC test des resultat des reponse Rest avec les methode GET PUT DELETE POST avec curl

FIXME: appel curl ou fopen d'une methode http depuis une action ou un controlleur.

TODO: sécuriser les accès HTTP1.1 par un fichier config similaire a l'applet Discourse faite pour Tinternet

TODO: ajouter un plug-in symfony permettant de charger un utilisateur dans les apps a partir de l'authentification multiple

TODO: lire les documentation officielles provenant des 4 plate-formes tranquillement afin de comprendre commet doit on tester ces type d'auth quitte a créé un sous domaine particulier directement hebergé sur gittea
-->Sécuriser le serveur de dev

Emmanuel ROY authored on 04/12/2019 15:21:19
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,211 @@
1
+<?php
2
+
3
+
4
+namespace MVC\Classe;
5
+
6
+
7
+/**
8
+ * Class Response
9
+ *
10
+ * example use:
11
+ * $data = array('a','b','c');
12
+ *
13
+ * Three Way to send a request
14
+ *
15
+ * $request = new Response('http://myurl','mymethod');
16
+ * $request->addContent($data);
17
+ * $request->send();
18
+ *
19
+ * OR
20
+ *
21
+ * $request = new Response('http://myurl');
22
+ * (
23
+ * $request->createContext('mymethod')
24
+ * $request->addContent($data);
25
+ * $request->send();
26
+ * ) OR (
27
+ * $request->get($data);
28
+ * $request->post($data);
29
+ * $request->put($data);
30
+ * $request->delete($data);
31
+ *
32
+ *
33
+ * OR
34
+ *
35
+ * $request = new Response();
36
+ * $request->setUrl('http://myurl')->get($data)
37
+ * $request->setUrl('http://myurl')->post($data)
38
+ * $request->setUrl('http://myurl')->put($data)
39
+ * $request->setUrl('http://myurl')->delete($data)
40
+ *
41
+ * @package MVC\Classe
42
+ */
43
+class HttpMethodRequete
44
+{
45
+    protected $url;
46
+    protected $options;
47
+    protected $method;
48
+    protected $data;
49
+    protected $content;
50
+
51
+    /**
52
+     * Response multi-constructor.
53
+     */
54
+    public function __construct()
55
+    {
56
+        $argumentFunction = func_get_args();
57
+        $nbParamsFunction = func_num_args();
58
+        if (method_exists($this, $function = '__construct' . $nbParamsFunction)) {
59
+            call_user_func_array(array($this, $function), $argumentFunction);
60
+        }
61
+    }
62
+
63
+    /**
64
+     * @return $this
65
+     */
66
+    public function __construct0()
67
+    {
68
+        return $this;
69
+    }
70
+
71
+    /**
72
+     * @param $url
73
+     * @return $this
74
+     */
75
+    public function __construct1($url)
76
+    {
77
+        $this->url = $url;
78
+        return $this;
79
+    }
80
+
81
+    /**
82
+     * Response constructor.
83
+     * @param $url URI
84
+     * @param $method POST,...
85
+     * @param $options
86
+     * @return $this
87
+     */
88
+    public function __construct2($url, $method)
89
+    {
90
+        $this->url = $url;
91
+        $this->method = $method;
92
+        // utilisez 'http' même si vous envoyez la requête sur https:// ...
93
+        $this->options = array(
94
+            'http' => array(
95
+                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
96
+                'method' => $method,
97
+            )
98
+        );
99
+        return $this;
100
+    }
101
+
102
+    public function setUrl($url)
103
+    {
104
+        $this->url = $url;
105
+        return $this;
106
+    }
107
+
108
+    /*
109
+        public function setGetParamsUrl($url, $params = array())
110
+        {
111
+            $this->url = $url . (strpos($url, '?') ? '&' : '?') . http_build_query($params);
112
+            return $this;
113
+        }
114
+    */
115
+    public function send()
116
+    {
117
+
118
+//        /** Pour utiliser ce code il faut mettre la variable allow_furl_open a ON dans php.ini */
119
+//
120
+//        ob_start();
121
+//        Dumper::dump($this->options);
122
+//        Dumper::dump($this->url);
123
+//        $text = ob_get_clean();
124
+//        Logger::addLog('fopen',$text);
125
+//
126
+//        $context = stream_context_create($this->options);
127
+//        $result = file_get_contents($this->url, false, $context);
128
+//        if ($result === FALSE) {
129
+//            /* Handle error */
130
+//            return false;
131
+//        } else {
132
+//            return true;
133
+//        }
134
+
135
+        $curl_cmd = "curl -i -X $this->method -H 'Content-Type: application/json' -d '$this->content' $this->url";
136
+
137
+        /** Pour utiliser ce code il faut utiliser la variable curl.cainfo dans php.ini */
138
+        Logger::addLog('curl', $curl_cmd);
139
+
140
+        $curl = curl_init($this->url);
141
+        //curl_setopt($curl, CURLOPT_HEADER, false);
142
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
143
+        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->method);
144
+        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->data));
145
+
146
+        $response = curl_exec($curl);
147
+        curl_close($curl);
148
+
149
+        if (!$response) {
150
+            return false;
151
+        } else {
152
+            return true;
153
+        }
154
+
155
+        /** Pour utiliser ce code il faut mettre la variable safe_mode a ON dans php.ini */
156
+        //exec($curl_cmd);
157
+
158
+
159
+    }
160
+
161
+    /**
162
+     * @param $data Array
163
+     */
164
+    public function addContent($data)
165
+    {
166
+        $this->data = $data;
167
+        $this->content = json_encode($data);
168
+        $this->options['http']['content'] = http_build_query($data);
169
+        return $this;
170
+    }
171
+
172
+    public function replaceContext($method)
173
+    {
174
+        $this->method = $method;
175
+        return $this->createContext($method);
176
+    }
177
+
178
+    public function createContext($method)
179
+    {
180
+        $this->method = $method;
181
+        // utilisez 'http' même si vous envoyez la requête sur https:// ...
182
+        $this->options = array(
183
+            'http' => array(
184
+                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
185
+                'method' => $method,
186
+            )
187
+        );
188
+        return $this;
189
+    }
190
+
191
+    public function get($params = array())
192
+    {
193
+        return $this->replaceContext('GET')->addContent($params)->send();
194
+    }
195
+
196
+    public function post($params = array())
197
+    {
198
+        return $this->replaceContext('POST')->addContent($params)->send();
199
+    }
200
+
201
+    public function put($params = array())
202
+    {
203
+        return $this->replaceContext('PUT')->addContent($params)->send();
204
+    }
205
+
206
+    public function delete($params = array())
207
+    {
208
+        return $this->replaceContext('DELETE')->addContent($params)->send();
209
+    }
210
+
211
+}
0 212
\ No newline at end of file