HttpConnection.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. return $options;
  38. }
  39. public function getRequest($uri = '', $query = [], $headers = [])
  40. {
  41. $options = $this->getOptions();
  42. if ($query) {
  43. $options["query"] = $query;
  44. }
  45. if ($headers) {
  46. $options["headers"] = $headers;
  47. }
  48. return $this->client->get($uri, $options);
  49. }
  50. public function postRequest($uri = '', $headers = [], $body = '')
  51. {
  52. $options = $this->getOptions();
  53. if ($headers) {
  54. $options["headers"] = $headers;
  55. }
  56. if ($body) {
  57. $options["form_params"] = $body;
  58. }
  59. return $this->client->post($uri, $options);
  60. }
  61. public function postRequestRaw($uri = '', $headers = [], $body = '')
  62. {
  63. $options = $this->getOptions();
  64. if ($headers) {
  65. $options["headers"] = $headers;
  66. }
  67. if ($body) {
  68. $options["body"] = $body;
  69. }
  70. return $this->client->post($uri, $options);
  71. }
  72. }