123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * SaaS服务商Sdk客户端
- */
- class SaaSSdkClient
- {
- const SDK_VERSION = 'PHP-3.2.9';
- private $agentAccessKey;
- private $agentAccessSecret;
- private $accessKey;
- private $accessSecret;
- private $serverUrl;
- private $proxy;
- const connectTimeout = 15000;
- const readTimeout = 60000;
- public function __get($property_name) {
- if (isset($this->$property_name)) {
- return($this->$property_name);
- } else {
- return(NULL);
- }
- }
- public function __construct() {
- $count = func_num_args();
- $args=func_get_args();
- if (method_exists($this,$f='__construct'.$count)) {
- call_user_func_array(array($this,$f),$args);
- }
- }
- public function __construct3($agentToken, $agentSecret, $serverUrl) {
- $this->agentAccessKey = $agentToken;
- $this->agentAccessSecret = $agentSecret;
- $this->serverUrl = $serverUrl;
- }
- public function __construct4($agentToken, $agentSecret, $serverUrl, $proxy) {
- $this->agentAccessKey = $agentToken;
- $this->agentAccessSecret = $agentSecret;
- $this->serverUrl = $serverUrl;
- $this->proxy = $proxy;
- }
- /**
- * HttpHeader constructor.
- * @param $timestamp
- * @param $accessToken
- * @param $signature
- * @param $version
- */
- public function __construct5($agentToken, $agentSecret, $accessKey, $accessSecret, $serverUrl) {
- $this->agentAccessKey = $agentToken;
- $this->agentAccessSecret = $agentSecret;
- $this->accessKey = $accessKey;
- $this->accessSecret = $accessSecret;
- $this->serverUrl = $serverUrl;
- }
- /**
- * SDK POST和GET请求调用封装
- *
- * @param request 请求参数
- * @return
- */
- public function service(SdkRequest $request) {
- $url = $this->serverUrl.$request->getUrl();
- $time = $this->get_millistime();
- $nonce = $this->get_guid();
-
- $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
- $agentSignature = md5(str_replace(' ', '',$this->agentAccessKey.$this->agentAccessSecret.$time.$nonce));
-
- $httpHeader = new HttpHeader($this->accessKey, $this->agentAccessKey, $time, $signature, $agentSignature,self::SDK_VERSION, $nonce);
- $httpHeader = $httpHeader->getAgentArray();
- $httpParamers = $request->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;
- }
- /**
- * 下载接口服务
- */
- public function download(SdkRequest $request, $outputStream) {
- $url = $this->serverUrl.$request->getUrl();
- $time = $this->get_millistime();
- $nonce = $this->get_guid();
- $signature = null;
- if(!empty($accessKey)) {
- $nonce = $this->get_guid();
- $signature = md5(str_replace(' ', '',$this->accessKey.$this->accessSecret.$time.$nonce));
- }
- $agentSignature = md5(str_replace(' ', '',$this->agentAccessKey.$this->agentAccessSecret.$time.$nonce));
- $httpHeader = new HttpHeader($this->accessKey, $this->agentAccessKey, $time, $signature, $agentSignature,self::SDK_VERSION, $nonce);
- $httpHeader = $httpHeader->getArray();
- $httpParamers = $request->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;
- }
- }
|