123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- /**
- *
- * SDK客户端封装类
- *
- */
- class SDKClient{
- const SDK_VERSION = 'PHP-3.2.9'; // php版本
- private $accessKey; // appToken
- private $accessSecret; // appSecret
- private $serverUrl; // 服务地址
- const connectTimeout = 15;
- const readTimeout = 30;
- public function __get($property_name) {
- if (isset($this->$property_name)) {
- return($this->$property_name);
- } else {
- return(NULL);
- }
- }
-
- public function __construct($accessKey,$accessSecret,$serverUrl) {
- $this->accessKey = $accessKey;
- $this->accessSecret = $accessSecret;
- $this->serverUrl = $serverUrl;
- }
- /**
- * 普通接口服务
- * @param SdkRequest $baseRequest
- * @return mixed|null|string
- */
- public function service(SdkRequest $baseRequest) {
- $url = $this->serverUrl.$baseRequest->getUrl();
- $time = $this->get_millistime();
- $nonce = $this->get_guid();
- $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
- $httpHeader = new HttpHeader($this->accessKey, $time, $signature, self::SDK_VERSION, $nonce);
- $httpHeader = $httpHeader->getArray();
- $httpParamers = $baseRequest->getHttpParamers();
- if($httpParamers->isJson()){
- $result = doServiceWithJson($url, $httpParamers->getJsonParams(), $httpHeader, self::connectTimeout, self::readTimeout);
- } else {
- $result = doService($url, $httpParamers, $httpHeader, self::connectTimeout, self::readTimeout);
- }
- if(strpos($url, 'download') === false ){
- $result = json_decode($result, true);
- }
- return $result;
- }
- /**
- * 下载接口服务
- * @param SdkRequest $baseRequest
- * @param $filePath
- * @return mixed|null|string
- */
- public function downloadService(SdkRequest $baseRequest, $filePath) {
- $url = $this->serverUrl.$baseRequest->getUrl();
- $time = $this->get_millistime();
- $nonce = $this->get_guid();
- $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
- $httpHeader = new HttpHeader($this->accessKey, $time, $signature, self::SDK_VERSION, $nonce);
- $httpHeader = $httpHeader->getArray();
- $httpParamers = $baseRequest->getHttpParamers();
- return doDownload($url, $httpParamers, $httpHeader, self::connectTimeout, self::readTimeout, $filePath);
- }
- /**
- * 获取当前时间戳
- * @return string
- */
- private function get_millistime(){
- $microtime = microtime();
- $comps = explode(' ', $microtime);
- return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
- }
- /**
- * 获取全局uuid
- * @return string
- */
- private function get_guid(){
- mt_srand((double) microtime() * 10000);
- $charid = md5(uniqid(rand(), true));
- $hyphen = chr(45); // “-”
- $uuid = substr($charid, 0, 8) . $hyphen
- . substr($charid, 8, 4) . $hyphen
- . substr($charid, 12, 4) . $hyphen
- . substr($charid, 16, 4) . $hyphen
- . substr($charid, 20, 12);
- return $uuid;
- }
- }
|