factorys = []; $this->ak = strval($config['key']); $this->sk = strval($config['secret']); if (isset($config['security_token'])) { $this->securityToken = strval($config['security_token']); } if (isset($config['endpoint'])) { $this->endpoint = trim(strval($config['endpoint'])); } if ($this->endpoint === '') { throw new \InvalidArgumentException('endpoint is not set'); } while ($this->endpoint[strlen($this->endpoint) - 1] === '/') { $this->endpoint = substr($this->endpoint, 0, strlen($this->endpoint) - 1); } if (strpos($this->endpoint, 'http') !== 0) { $this->endpoint = 'https://' . $this->endpoint; } if (isset($config['signature'])) { $this->signature = strval($config['signature']); } if (isset($config['path_style'])) { $this->pathStyle = $config['path_style']; } if (isset($config['region'])) { $this->region = strval($config['region']); } if (isset($config['ssl_verify'])) { $this->sslVerify = $config['ssl_verify']; } elseif (isset($config['ssl.certificate_authority'])) { $this->sslVerify = $config['ssl.certificate_authority']; } else { // nothing handle } if (isset($config['max_retry_count'])) { $this->maxRetryCount = intval($config['max_retry_count']); } if (isset($config['timeout'])) { $this->timeout = intval($config['timeout']); } if (isset($config['socket_timeout'])) { $this->socketTimeout = intval($config['socket_timeout']); } if (isset($config['connect_timeout'])) { $this->connectTimeout = intval($config['connect_timeout']); } if (isset($config['chunk_size'])) { $this->chunkSize = intval($config['chunk_size']); } if (isset($config['exception_response_mode'])) { $this->exceptionResponseMode = $config['exception_response_mode']; } if (isset($config['is_cname'])) { $this->isCname = $config['is_cname']; } $host = parse_url($this->endpoint)['host']; if (filter_var($host, FILTER_VALIDATE_IP) !== false) { $this->pathStyle = true; } $handler = self::chooseHandler($this); $this->httpClient = self::createHttpClient($handler); } private function createHttpClient($handler) { return new Client( [ 'timeout' => 0, 'read_timeout' => $this->socketTimeout, 'connect_timeout' => $this->connectTimeout, 'allow_redirects' => false, 'verify' => $this->sslVerify, 'expect' => false, 'handler' => HandlerStack::create($handler), 'curl' => [ CURLOPT_BUFFERSIZE => $this->chunkSize, ], ] ); } public function __destruct() { $this->close(); } public function refresh($key, $secret, $securityToken = false) { $this->ak = strval($key); $this->sk = strval($secret); if ($securityToken) { $this->securityToken = strval($securityToken); } } /** * Get the default User-Agent string to use with Guzzle * * @return string */ public static function getDefaultUserAgent() { static $defaultAgent = ''; if (!$defaultAgent) { $defaultAgent = 'obs-sdk-php/' . self::SDK_VERSION; } return $defaultAgent; } /** * Factory method to create a new Obs client using an array of configuration options. * * @param array $config Client configuration data * * @return ObsClient */ public static function factory(array $config = []) { return new ObsClient($config); } public function close() { if ($this->factorys) { foreach ($this->factorys as $factory) { $factory->close(); } } } public function initLog(array $logConfig = []) { ObsLog::initLog($logConfig); $msg = []; $msg[] = '[OBS SDK Version=' . self::SDK_VERSION; $msg[] = 'Endpoint=' . $this->endpoint; $msg[] = 'Access Mode=' . ($this->pathStyle ? 'Path' : 'Virtual Hosting') . ']'; ObsLog::commonLog(WARNING, implode("];[", $msg)); } private static function chooseHandler($obsclient) { $handler = null; if (function_exists('curl_multi_exec') && function_exists('curl_exec')) { $f1 = new SdkCurlFactory(50); $f2 = new SdkCurlFactory(3); $obsclient->factorys[] = $f1; $obsclient->factorys[] = $f2; $handler = Proxy::wrapSync( new CurlMultiHandler(['handle_factory' => $f1]), new CurlHandler(['handle_factory' => $f2]) ); } elseif (function_exists('curl_exec')) { $f = new SdkCurlFactory(3); $obsclient->factorys[] = $f; $handler = new CurlHandler(['handle_factory' => $f]); } elseif (function_exists('curl_multi_exec')) { $f = new SdkCurlFactory(50); $obsclient->factorys[] = $f; $handler = new CurlMultiHandler(['handle_factory' => $f]); } else { // nothing handle } if (ini_get('allow_url_fopen')) { $handler = $handler ? Proxy::wrapStreaming($handler, new SdkStreamHandler()) : new SdkStreamHandler(); } elseif (!$handler) { throw new \UnexpectedValueException( 'GuzzleHttp requires cURL, the allow_url_fopen ini setting, or a custom HTTP handler.'); } else { // nothing handle } return $handler; } }