SaaSSdkClient.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * SaaS服务商Sdk客户端
  4. */
  5. class SaaSSdkClient
  6. {
  7. const SDK_VERSION = 'PHP-3.2.9';
  8. private $agentAccessKey;
  9. private $agentAccessSecret;
  10. private $accessKey;
  11. private $accessSecret;
  12. private $serverUrl;
  13. private $proxy;
  14. const connectTimeout = 15000;
  15. const readTimeout = 60000;
  16. public function __get($property_name) {
  17. if (isset($this->$property_name)) {
  18. return($this->$property_name);
  19. } else {
  20. return(NULL);
  21. }
  22. }
  23. public function __construct() {
  24. $count = func_num_args();
  25. $args=func_get_args();
  26. if (method_exists($this,$f='__construct'.$count)) {
  27. call_user_func_array(array($this,$f),$args);
  28. }
  29. }
  30. public function __construct3($agentToken, $agentSecret, $serverUrl) {
  31. $this->agentAccessKey = $agentToken;
  32. $this->agentAccessSecret = $agentSecret;
  33. $this->serverUrl = $serverUrl;
  34. }
  35. public function __construct4($agentToken, $agentSecret, $serverUrl, $proxy) {
  36. $this->agentAccessKey = $agentToken;
  37. $this->agentAccessSecret = $agentSecret;
  38. $this->serverUrl = $serverUrl;
  39. $this->proxy = $proxy;
  40. }
  41. /**
  42. * HttpHeader constructor.
  43. * @param $timestamp
  44. * @param $accessToken
  45. * @param $signature
  46. * @param $version
  47. */
  48. public function __construct5($agentToken, $agentSecret, $accessKey, $accessSecret, $serverUrl) {
  49. $this->agentAccessKey = $agentToken;
  50. $this->agentAccessSecret = $agentSecret;
  51. $this->accessKey = $accessKey;
  52. $this->accessSecret = $accessSecret;
  53. $this->serverUrl = $serverUrl;
  54. }
  55. /**
  56. * SDK POST和GET请求调用封装
  57. *
  58. * @param request 请求参数
  59. * @return
  60. */
  61. public function service(SdkRequest $request) {
  62. $url = $this->serverUrl.$request->getUrl();
  63. $time = $this->get_millistime();
  64. $nonce = $this->get_guid();
  65. $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
  66. $agentSignature = md5(str_replace(' ', '',$this->agentAccessKey.$this->agentAccessSecret.$time.$nonce));
  67. $httpHeader = new HttpHeader($this->accessKey, $this->agentAccessKey, $time, $signature, $agentSignature,self::SDK_VERSION, $nonce);
  68. $httpHeader = $httpHeader->getAgentArray();
  69. $httpParamers = $request->getHttpParamers();
  70. if($httpParamers->isJson()){
  71. $result = doServiceWithJson($url, $httpParamers->getJsonParams(), $httpHeader, self::connectTimeout, self::readTimeout);
  72. } else {
  73. $result = doService($url, $httpParamers, $httpHeader, self::connectTimeout, self::readTimeout);
  74. }
  75. if(strpos($url, 'download') === false ){
  76. $result = json_decode($result, true);
  77. }
  78. return $result;
  79. }
  80. /**
  81. * 下载接口服务
  82. */
  83. public function download(SdkRequest $request, $outputStream) {
  84. $url = $this->serverUrl.$request->getUrl();
  85. $time = $this->get_millistime();
  86. $nonce = $this->get_guid();
  87. $signature = null;
  88. if(!empty($accessKey)) {
  89. $nonce = $this->get_guid();
  90. $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
  91. }
  92. $agentSignature = md5(str_replace(' ', '',$this->agentAccessKey.$this->agentAccessSecret.$time.$nonce));
  93. $httpHeader = new HttpHeader($this->accessKey, $this->agentAccessKey, $time, $signature, $agentSignature,self::SDK_VERSION, $nonce);
  94. $httpHeader = $httpHeader->getArray();
  95. $httpParamers = $request->getHttpParamers();
  96. return doDownload($url, $httpParamers, $httpHeader, self::connectTimeout, self::readTimeout, $filePath);
  97. }
  98. /**
  99. * 获取当前时间戳
  100. * @return string
  101. */
  102. private function get_millistime(){
  103. $microtime = microtime();
  104. $comps = explode(' ', $microtime);
  105. return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
  106. }
  107. /**
  108. * 获取全局uuid
  109. * @return string
  110. */
  111. private function get_guid(){
  112. mt_srand((double) microtime() * 10000);
  113. $charid = md5(uniqid(rand(), true));
  114. $hyphen = chr(45); // “-”
  115. $uuid = substr($charid, 0, 8) . $hyphen
  116. . substr($charid, 8, 4) . $hyphen
  117. . substr($charid, 12, 4) . $hyphen
  118. . substr($charid, 16, 4) . $hyphen
  119. . substr($charid, 20, 12);
  120. return $uuid;
  121. }
  122. }