You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpRequestCommand.php 1.6KB

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