HttpConnection.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing,
  12. * software distributed under the License is distributed on an
  13. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either express or implied. See the License for the
  15. * specific language governing permissions and limitations
  16. * under the License.
  17. */
  18. namespace TencentCloud\Common\Http;
  19. use GuzzleHttp\Client;
  20. /**
  21. * http连接类
  22. * @package TencentCloud\Common\http
  23. */
  24. class HttpConnection
  25. {
  26. private $client;
  27. private $profile;
  28. function __construct($url, $profile)
  29. {
  30. $this->client = new Client(["base_uri" => $url]);
  31. $this->profile = $profile;
  32. }
  33. private function getOptions()
  34. {
  35. $options = ["allow_redirects" => false];
  36. $options["timeout"] = $this->profile->getHttpProfile()->getReqTimeout();
  37. $options["proxy"] = $this->profile->getHttpProfile()->getProxy();
  38. return $options;
  39. }
  40. public function getRequest($uri = '', $query = [], $headers = [])
  41. {
  42. $options = $this->getOptions();
  43. if ($query) {
  44. $options["query"] = $query;
  45. }
  46. if ($headers) {
  47. $options["headers"] = $headers;
  48. }
  49. return $this->client->get($uri, $options);
  50. }
  51. public function postRequest($uri = '', $headers = [], $body = '')
  52. {
  53. $options = $this->getOptions();
  54. if ($headers) {
  55. $options["headers"] = $headers;
  56. }
  57. if ($body) {
  58. $options["form_params"] = $body;
  59. }
  60. return $this->client->post($uri, $options);
  61. }
  62. public function postRequestRaw($uri = '', $headers = [], $body = '')
  63. {
  64. $options = $this->getOptions();
  65. if ($headers) {
  66. $options["headers"] = $headers;
  67. }
  68. if ($body) {
  69. $options["body"] = $body;
  70. }
  71. return $this->client->post($uri, $options);
  72. }
  73. }