SDKClient.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. *
  4. * SDK客户端封装类
  5. *
  6. */
  7. class SDKClient{
  8. const SDK_VERSION = 'PHP-3.2.9'; // php版本
  9. private $accessKey; // appToken
  10. private $accessSecret; // appSecret
  11. private $serverUrl; // 服务地址
  12. const connectTimeout = 15;
  13. const readTimeout = 30;
  14. public function __get($property_name) {
  15. if (isset($this->$property_name)) {
  16. return($this->$property_name);
  17. } else {
  18. return(NULL);
  19. }
  20. }
  21. public function __construct($accessKey,$accessSecret,$serverUrl) {
  22. $this->accessKey = $accessKey;
  23. $this->accessSecret = $accessSecret;
  24. $this->serverUrl = $serverUrl;
  25. }
  26. /**
  27. * 普通接口服务
  28. * @param SdkRequest $baseRequest
  29. * @return mixed|null|string
  30. */
  31. public function service(SdkRequest $baseRequest) {
  32. $url = $this->serverUrl.$baseRequest->getUrl();
  33. $time = $this->get_millistime();
  34. $nonce = $this->get_guid();
  35. $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
  36. $httpHeader = new HttpHeader($this->accessKey, $time, $signature, self::SDK_VERSION, $nonce);
  37. $httpHeader = $httpHeader->getArray();
  38. $httpParamers = $baseRequest->getHttpParamers();
  39. if($httpParamers->isJson()){
  40. $result = doServiceWithJson($url, $httpParamers->getJsonParams(), $httpHeader, self::connectTimeout, self::readTimeout);
  41. } else {
  42. $result = doService($url, $httpParamers, $httpHeader, self::connectTimeout, self::readTimeout);
  43. }
  44. if(strpos($url, 'download') === false ){
  45. $result = json_decode($result, true);
  46. }
  47. return $result;
  48. }
  49. /**
  50. * 下载接口服务
  51. * @param SdkRequest $baseRequest
  52. * @param $filePath
  53. * @return mixed|null|string
  54. */
  55. public function downloadService(SdkRequest $baseRequest, $filePath) {
  56. $url = $this->serverUrl.$baseRequest->getUrl();
  57. $time = $this->get_millistime();
  58. $nonce = $this->get_guid();
  59. $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
  60. $httpHeader = new HttpHeader($this->accessKey, $time, $signature, self::SDK_VERSION, $nonce);
  61. $httpHeader = $httpHeader->getArray();
  62. $httpParamers = $baseRequest->getHttpParamers();
  63. return doDownload($url, $httpParamers, $httpHeader, self::connectTimeout, self::readTimeout, $filePath);
  64. }
  65. /**
  66. * 获取当前时间戳
  67. * @return string
  68. */
  69. private function get_millistime(){
  70. $microtime = microtime();
  71. $comps = explode(' ', $microtime);
  72. return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
  73. }
  74. /**
  75. * 获取全局uuid
  76. * @return string
  77. */
  78. private function get_guid(){
  79. mt_srand((double) microtime() * 10000);
  80. $charid = md5(uniqid(rand(), true));
  81. $hyphen = chr(45); // “-”
  82. $uuid = substr($charid, 0, 8) . $hyphen
  83. . substr($charid, 8, 4) . $hyphen
  84. . substr($charid, 12, 4) . $hyphen
  85. . substr($charid, 16, 4) . $hyphen
  86. . substr($charid, 20, 12);
  87. return $uuid;
  88. }
  89. }