|
@@ -0,0 +1,58 @@
|
|
1
|
+<?php
|
|
2
|
+/**
|
|
3
|
+ * Created by PhpStorm.
|
|
4
|
+ * User: robin
|
|
5
|
+ * Date: 11/24/15
|
|
6
|
+ * Time: 12:15 AM
|
|
7
|
+ */
|
|
8
|
+
|
|
9
|
+namespace App\Http\Business\Commands;
|
|
10
|
+
|
|
11
|
+use GuzzleHttp\Client;
|
|
12
|
+use GuzzleHttp\Exception\ClientException;
|
|
13
|
+use GuzzleHttp\Exception\RequestException;
|
|
14
|
+use Luticate\Utils\LuLog;
|
|
15
|
+
|
|
16
|
+class HttpRequestCommand extends AbstractCommand
|
|
17
|
+{
|
|
18
|
+ /**
|
|
19
|
+ * @return bool
|
|
20
|
+ */
|
|
21
|
+ public function exec()
|
|
22
|
+ {
|
|
23
|
+ $data = $this->_command->getData();
|
|
24
|
+ $url = $data["Url"];
|
|
25
|
+ $method = $data["Method"];
|
|
26
|
+ $options = $data["Options"];
|
|
27
|
+
|
|
28
|
+ $client = new Client();
|
|
29
|
+
|
|
30
|
+ try {
|
|
31
|
+ $response = $client->request($method, $url, $options);
|
|
32
|
+ } catch (ClientException $e) {
|
|
33
|
+ $response = $e->getResponse();
|
|
34
|
+ if (!is_null($response)) {
|
|
35
|
+ LuLog::log($response->getBody());
|
|
36
|
+ }
|
|
37
|
+ LuLog::log($e);
|
|
38
|
+ abort(500, "Failed to execute request (client)");
|
|
39
|
+ } catch (RequestException $e) {
|
|
40
|
+ $response = $e->getResponse();
|
|
41
|
+ if (!is_null($response)) {
|
|
42
|
+ LuLog::log($response->getBody());
|
|
43
|
+ }
|
|
44
|
+ LuLog::log($e);
|
|
45
|
+ abort(500, "Failed to execute request (request)");
|
|
46
|
+ } catch (\Exception $e) {
|
|
47
|
+ LuLog::log($e);
|
|
48
|
+ abort(500, "Failed to execute request (unknown)");
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ if ($response->getStatusCode() != 200)
|
|
52
|
+ {
|
|
53
|
+ abort(500, "Failed to execute request (" . $response->getStatusCode() . ")");
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ return true;
|
|
57
|
+ }
|
|
58
|
+}
|