初始化代码

This commit is contained in:
2025-12-22 14:32:54 +08:00
parent e27ab90d9f
commit d02b31a8b9
1459 changed files with 240973 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
<?php
/**
* Signature create related functions.
*/
namespace QcloudImage;
/**
* Auth class for creating reusable signature.
*/
class Auth {
public function __construct($appId, $secretId, $secretKey) {
$this->appId = $appId;
$this->secretId = $secretId;
$this->secretKey = $secretKey;
}
/**
* Return the appId
*/
public function getAppId() {
return $this->appId;
}
/**
* Create reusable signature.
* This signature will expire at time()+$howlong timestamp.
* Return the signature on success.
* Return false on fail.
*/
public function getSign($bucket, $howlong = 30) {
if ($howlong <= 0) {
return false;
}
$now = time();
$expiration = $now + $howlong;
$random = rand();
$plainText = "a=".$this->appId."&b=$bucket&k=".$this->secretId."&e=$expiration&t=$now&r=$random&f=";
$bin = hash_hmac('SHA1', $plainText, $this->secretKey, true);
return base64_encode($bin.$plainText);
}
private $appId = "";
private $secretId = "";
private $secretKey = "";
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,55 @@
<?php
/**
* Some settings for SDK.
*/
namespace QcloudImage;
/**
* Conf class.
*/
class Conf {
const VERSION = '2.0.0';
const SERVER_ADDR = 'service.image.myqcloud.com';
const SERVER_ADDR2 = 'recognition.image.myqcloud.com';
private $HOST = self::SERVER_ADDR2;
private $REQ_TIMEOUT = 60;
private $SCHEME = 'https';
public function useHttp() {
$this->SCHEME = 'http';
}
public function useHttps() {
$this->SCHEME = 'https';
}
public function setTimeout($timeout) {
if ($timeout > 0) {
$this->REQ_TIMEOUT = $timeout;
}
}
public function useNewDomain()
{
$this->HOST = self::SERVER_ADDR2;
}
public function useOldDomain()
{
$this->HOST = self::SERVER_ADDR;
}
public function timeout() {
return $this->REQ_TIMEOUT;
}
public function buildUrl($uri) {
return $this->SCHEME . '://' . $this->HOST . '/' . ltrim($uri, "/");
}
public static function getUa($appid = null) {
$ua = 'CIPhpSDK/'.self::VERSION.' ('.php_uname().')';
if ($appid) {
$ua .= " User($appid)";
}
return $ua;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* Error defination.
*/
namespace QcloudImage;
class Error {
/**
* Create reusable signature.
* This signature will expire at time()+$howlong timestamp.
* Return the signature on success.
* Return false on fail.
*/
public static function json($code, $message, $httpcode = 0) {
return json_encode(array(
'code' => $code,
'message' => $message,
'httpcode' => $httpcode,
'data' => json_decode('{}',true)
));
}
public static $Param = -1;
public static $Network = -2;
public static $FilePath = -3;
public static $Unknown = -4;
}

View File

@@ -0,0 +1,122 @@
<?php
/**
* Http Client use curl.
*/
namespace QcloudImage;
function my_curl_reset($handler) {
curl_setopt($handler, CURLOPT_URL, '');
curl_setopt($handler, CURLOPT_HTTPHEADER, array());
curl_setopt($handler, CURLOPT_POSTFIELDS, array());
curl_setopt($handler, CURLOPT_TIMEOUT, 0);
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($handler, CURLOPT_PROXY, null);
}
class HttpClient {
private $proxy = '';
public function setProxy($proxy)
{
$this->proxy = $proxy;
}
public function __destory() {
if ($this->curlHandler) {
curl_close($this->curlHandler);
}
}
/**
* send http request
* @param array $request http请求信息
* url : 请求的url地址
* method : 请求方法,'get', 'post', 'put', 'delete', 'head'
* data : 请求数据如有设置则method为post
* header : 需要设置的http头部
* host : 请求头部host
* timeout : 请求超时时间
* cert : ca文件路径
* ssl_version: SSL版本号
* @return string http请求响应
*/
public function sendRequest($request) {
if (!is_array($request) || !isset($request["url"])) {
return false;
}
if ($this->curlHandler) {
if (function_exists('curl_reset')) {
curl_reset($this->curlHandler);
} else {
my_curl_reset($this->curlHandler);
}
} else {
$this->curlHandler = curl_init();
}
curl_setopt($this->curlHandler, CURLOPT_URL, $request['url']);
$method = 'GET';
if (isset($request['method']) &&
in_array(strtolower($request['method']), array('get', 'post', 'put', 'delete', 'head'))) {
$method = strtoupper($request['method']);
} else if (isset($request['data'])) {
$method = 'POST';
}
$header = isset($request['header']) ? $request['header'] : array();
$header[] = 'Connection: keep-alive';
if ('POST' == $method) {
$header[] = 'Expect: ';
}
isset($request['host']) && $header[] = 'Host:' . $request['host'];
curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER, $header);
if (empty($this->proxy)) {
curl_setopt($this->curlHandler, CURLOPT_PROXY, null);
} else {
curl_setopt($this->curlHandler, CURLOPT_PROXY, $this->proxy);
}
curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curlHandler, CURLOPT_CUSTOMREQUEST, $method);
isset($request['timeout']) && curl_setopt($this->curlHandler, CURLOPT_TIMEOUT, $request['timeout']);
isset($request['data']) && in_array($method, array('POST', 'PUT')) &&
curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS, $request['data']);
$ssl = substr($request['url'], 0, 8) == "https://" ? true : false;
if (isset($request['cert'])) {
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($this->curlHandler, CURLOPT_CAINFO, $request['cert']);
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYHOST,2);
if (isset($request['ssl_version'])) {
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, $request['ssl_version']);
} else {
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, 4);
}
} else if ($ssl) {
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYPEER,true); //true any ca
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYHOST,2);
if (isset($request['ssl_version'])) {
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, $request['ssl_version']);
} else {
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, 4);
}
}
$ret = curl_exec($this->curlHandler);
$this->httpInfo = curl_getinfo($this->curlHandler);
return $ret;
}
public function statusCode() {
if ($this->httpInfo) {
return $this->httpInfo['http_code'];
}
return 0;
}
private $httpInfo;
private $curlHandler;
}