GTBaseApi.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. class GTBaseApi
  3. {
  4. protected $gtClient;
  5. protected function post($api, $params)
  6. {
  7. return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_POST);
  8. }
  9. protected function put($api, $params)
  10. {
  11. return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_PUT);
  12. }
  13. protected function get($api, $params)
  14. {
  15. return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_GET);
  16. }
  17. protected function delete($api, $params)
  18. {
  19. return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_DELETE);
  20. }
  21. private function httpRequest($api, $params, $method, $gzip = false)
  22. {
  23. try {
  24. $rep = GTHttpManager::httpRequest($this->getUrl($api), $params, $this->buildHead(), $gzip, $method);
  25. } catch (GTException $e) {
  26. throw $e;
  27. }
  28. if ($rep != null) {
  29. if ('10001' == $rep['code']) {
  30. try {
  31. if ($this->gtClient->auth()) {
  32. $rep = GTHttpManager::httpRequest($this->getUrl($api), $params, $this->buildHead(), $gzip, $method);
  33. }
  34. } catch (GTException $e) {
  35. throw $e;
  36. }
  37. } else if ('301' == $rep['code']) {
  38. if (empty($rep["data"]) || empty($rep["data"]["host_list"]) || empty($rep["data"]["host_list"][0]["domain_list"])) {
  39. throw new GTException("域名错误");
  40. }
  41. $this->gtClient->setDomainUrlList($rep["data"]["host_list"][0]["domain_list"]);
  42. try {
  43. $rep = GTHttpManager::httpRequest($this->getUrl($api), $params, $this->buildHead(), $gzip, $method);
  44. } catch (GTException $e) {
  45. throw $e;
  46. }
  47. }
  48. }
  49. return $rep;
  50. }
  51. private function analysisUrlList()
  52. {
  53. }
  54. private function buildHead()
  55. {
  56. $headers = array();
  57. if ($this->gtClient->getAuthToken() != null) {
  58. array_push($headers, "token:" . $this->gtClient->getAuthToken());
  59. }
  60. return $headers;
  61. }
  62. private function getUrl($api)
  63. {
  64. return $this->gtClient->getHost() . "/v2/" . $this->gtClient->getAppId() . $api;
  65. }
  66. }