daryast

Untitled

Apr 6th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. <?php
  2. use Guzzle\Http\Client;
  3. use Guzzle\Plugin\Log\LogPlugin;
  4. use /** @noinspection PhpUndefinedNamespaceInspection */
  5. Guzzle\Log\MessageFormatter;
  6. use /** @noinspection PhpUndefinedNamespaceInspection */
  7. Guzzle\Http\Exception\ClientErrorResponseException;
  8.  
  9. use /** @noinspection PhpUndefinedNamespaceInspection */
  10. Guzzle\Http\Exception\RequestException;
  11. use Guzzle\Http\Message\Response;
  12.  
  13.  
  14. class HttpClient
  15. {
  16. public $http;
  17. public $request;
  18.  
  19. /**
  20. * HttpClient
  21. */
  22. public function __construct()
  23. {
  24. $this->init();
  25.  
  26. }
  27.  
  28. /**
  29. * Инициилизация httpClient
  30. */
  31. public function init()
  32. {
  33. $this->http = new Client();
  34. }
  35.  
  36.  
  37. /**
  38. * метод post
  39. * $uri - url
  40. */
  41. public function post($uri = null, $headers = null, $postBody = null, array $options)
  42. {
  43. $this->request = $this->http->post($uri, $headers, $postBody, array('exceptions' => false))->send();
  44. return $this;
  45. }
  46.  
  47. /**
  48. * метод get
  49. */
  50. public function get($uri = null, $headers = null, array $options)
  51. {
  52. $this->request = $this->http->get($uri, $headers, array('exceptions' => false))->send();
  53. return $this;
  54. }
  55.  
  56. /**
  57. * получение заголовков ответа
  58. */
  59. public function getHeader()
  60. {
  61. if ($this->request instanceof Response) {
  62. return $this->request->getRawHeaders();
  63. } else throw new Exception("Ошибка: запрос не был отправлен");
  64. }
  65.  
  66.  
  67. /**
  68. * получение тела ответа
  69. */
  70. public function getBody()
  71. {
  72. if ($this->request instanceof Response) {
  73. return $this->request->getBody();
  74. } else throw new Exception("Ошибка: запрос не был отправлен");
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment