初始化仓库
This commit is contained in:
160
vendor/qiniu/php-sdk/src/Qiniu/Http/Client.php
vendored
Normal file
160
vendor/qiniu/php-sdk/src/Qiniu/Http/Client.php
vendored
Normal file
@ -0,0 +1,160 @@
|
||||
<?php
|
||||
namespace Qiniu\Http;
|
||||
|
||||
use Qiniu\Config;
|
||||
use Qiniu\Http\Request;
|
||||
use Qiniu\Http\Response;
|
||||
|
||||
final class Client
|
||||
{
|
||||
public static function get($url, array $headers = array())
|
||||
{
|
||||
$request = new Request('GET', $url, $headers);
|
||||
return self::sendRequest($request);
|
||||
}
|
||||
|
||||
public static function delete($url, array $headers = array())
|
||||
{
|
||||
$request = new Request('DELETE', $url, $headers);
|
||||
return self::sendRequest($request);
|
||||
}
|
||||
|
||||
public static function post($url, $body, array $headers = array())
|
||||
{
|
||||
$request = new Request('POST', $url, $headers, $body);
|
||||
return self::sendRequest($request);
|
||||
}
|
||||
|
||||
public static function PUT($url, $body, array $headers = array())
|
||||
{
|
||||
$request = new Request('PUT', $url, $headers, $body);
|
||||
return self::sendRequest($request);
|
||||
}
|
||||
|
||||
public static function multipartPost(
|
||||
$url,
|
||||
$fields,
|
||||
$name,
|
||||
$fileName,
|
||||
$fileBody,
|
||||
$mimeType = null,
|
||||
array $headers = array()
|
||||
) {
|
||||
$data = array();
|
||||
$mimeBoundary = md5(microtime());
|
||||
|
||||
foreach ($fields as $key => $val) {
|
||||
array_push($data, '--' . $mimeBoundary);
|
||||
array_push($data, "Content-Disposition: form-data; name=\"$key\"");
|
||||
array_push($data, '');
|
||||
array_push($data, $val);
|
||||
}
|
||||
|
||||
array_push($data, '--' . $mimeBoundary);
|
||||
$finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
|
||||
$finalFileName = self::escapeQuotes($fileName);
|
||||
array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\"");
|
||||
array_push($data, "Content-Type: $finalMimeType");
|
||||
array_push($data, '');
|
||||
array_push($data, $fileBody);
|
||||
|
||||
array_push($data, '--' . $mimeBoundary . '--');
|
||||
array_push($data, '');
|
||||
|
||||
$body = implode("\r\n", $data);
|
||||
// var_dump($data);exit;
|
||||
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
|
||||
$headers['Content-Type'] = $contentType;
|
||||
$request = new Request('POST', $url, $headers, $body);
|
||||
return self::sendRequest($request);
|
||||
}
|
||||
|
||||
private static function userAgent()
|
||||
{
|
||||
$sdkInfo = "QiniuPHP/" . Config::SDK_VER;
|
||||
|
||||
$systemInfo = php_uname("s");
|
||||
$machineInfo = php_uname("m");
|
||||
|
||||
$envInfo = "($systemInfo/$machineInfo)";
|
||||
|
||||
$phpVer = phpversion();
|
||||
|
||||
$ua = "$sdkInfo $envInfo PHP/$phpVer";
|
||||
return $ua;
|
||||
}
|
||||
|
||||
public static function sendRequest($request)
|
||||
{
|
||||
$t1 = microtime(true);
|
||||
$ch = curl_init();
|
||||
$options = array(
|
||||
CURLOPT_USERAGENT => self::userAgent(),
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_SSL_VERIFYHOST => false,
|
||||
CURLOPT_HEADER => true,
|
||||
CURLOPT_NOBODY => false,
|
||||
CURLOPT_CUSTOMREQUEST => $request->method,
|
||||
CURLOPT_URL => $request->url,
|
||||
);
|
||||
// Handle open_basedir & safe mode
|
||||
if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
|
||||
$options[CURLOPT_FOLLOWLOCATION] = true;
|
||||
}
|
||||
if (!empty($request->headers)) {
|
||||
$headers = array();
|
||||
foreach ($request->headers as $key => $val) {
|
||||
array_push($headers, "$key: $val");
|
||||
}
|
||||
$options[CURLOPT_HTTPHEADER] = $headers;
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
|
||||
if (!empty($request->body)) {
|
||||
$options[CURLOPT_POSTFIELDS] = $request->body;
|
||||
}
|
||||
curl_setopt_array($ch, $options);
|
||||
$result = curl_exec($ch);
|
||||
$t2 = microtime(true);
|
||||
$duration = round($t2 - $t1, 3);
|
||||
$ret = curl_errno($ch);
|
||||
if ($ret !== 0) {
|
||||
$r = new Response(-1, $duration, array(), null, curl_error($ch));
|
||||
curl_close($ch);
|
||||
return $r;
|
||||
}
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
$headers = self::parseHeaders(substr($result, 0, $header_size));
|
||||
$body = substr($result, $header_size);
|
||||
curl_close($ch);
|
||||
return new Response($code, $duration, $headers, $body, null);
|
||||
}
|
||||
|
||||
private static function parseHeaders($raw)
|
||||
{
|
||||
$headers = array();
|
||||
$headerLines = explode("\r\n", $raw);
|
||||
foreach ($headerLines as $line) {
|
||||
$headerLine = trim($line);
|
||||
$kv = explode(':', $headerLine);
|
||||
if (count($kv) > 1) {
|
||||
$kv[0] =self::ucwordsHyphen($kv[0]);
|
||||
$headers[$kv[0]] = trim($kv[1]);
|
||||
}
|
||||
}
|
||||
return $headers;
|
||||
}
|
||||
|
||||
private static function escapeQuotes($str)
|
||||
{
|
||||
$find = array("\\", "\"");
|
||||
$replace = array("\\\\", "\\\"");
|
||||
return str_replace($find, $replace, $str);
|
||||
}
|
||||
|
||||
private static function ucwordsHyphen($str)
|
||||
{
|
||||
return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));
|
||||
}
|
||||
}
|
||||
35
vendor/qiniu/php-sdk/src/Qiniu/Http/Error.php
vendored
Normal file
35
vendor/qiniu/php-sdk/src/Qiniu/Http/Error.php
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace Qiniu\Http;
|
||||
|
||||
/**
|
||||
* 七牛业务请求逻辑错误封装类,主要用来解析API请求返回如下的内容:
|
||||
* <pre>
|
||||
* {"error" : "detailed error message"}
|
||||
* </pre>
|
||||
*/
|
||||
final class Error
|
||||
{
|
||||
private $url;
|
||||
private $response;
|
||||
|
||||
public function __construct($url, $response)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function code()
|
||||
{
|
||||
return $this->response->statusCode;
|
||||
}
|
||||
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
public function message()
|
||||
{
|
||||
return $this->response->error;
|
||||
}
|
||||
}
|
||||
18
vendor/qiniu/php-sdk/src/Qiniu/Http/Request.php
vendored
Normal file
18
vendor/qiniu/php-sdk/src/Qiniu/Http/Request.php
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Qiniu\Http;
|
||||
|
||||
final class Request
|
||||
{
|
||||
public $url;
|
||||
public $headers;
|
||||
public $body;
|
||||
public $method;
|
||||
|
||||
public function __construct($method, $url, array $headers = array(), $body = null)
|
||||
{
|
||||
$this->method = strtoupper($method);
|
||||
$this->url = $url;
|
||||
$this->headers = $headers;
|
||||
$this->body = $body;
|
||||
}
|
||||
}
|
||||
176
vendor/qiniu/php-sdk/src/Qiniu/Http/Response.php
vendored
Normal file
176
vendor/qiniu/php-sdk/src/Qiniu/Http/Response.php
vendored
Normal file
@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace Qiniu\Http;
|
||||
|
||||
/**
|
||||
* HTTP response Object
|
||||
*/
|
||||
final class Response
|
||||
{
|
||||
public $statusCode;
|
||||
public $headers;
|
||||
public $body;
|
||||
public $error;
|
||||
private $jsonData;
|
||||
public $duration;
|
||||
|
||||
/** @var array Mapping of status codes to reason phrases */
|
||||
private static $statusTexts = array(
|
||||
100 => 'Continue',
|
||||
101 => 'Switching Protocols',
|
||||
102 => 'Processing',
|
||||
200 => 'OK',
|
||||
201 => 'Created',
|
||||
202 => 'Accepted',
|
||||
203 => 'Non-Authoritative Information',
|
||||
204 => 'No Content',
|
||||
205 => 'Reset Content',
|
||||
206 => 'Partial Content',
|
||||
207 => 'Multi-Status',
|
||||
208 => 'Already Reported',
|
||||
226 => 'IM Used',
|
||||
300 => 'Multiple Choices',
|
||||
301 => 'Moved Permanently',
|
||||
302 => 'Found',
|
||||
303 => 'See Other',
|
||||
304 => 'Not Modified',
|
||||
305 => 'Use Proxy',
|
||||
307 => 'Temporary Redirect',
|
||||
308 => 'Permanent Redirect',
|
||||
400 => 'Bad Request',
|
||||
401 => 'Unauthorized',
|
||||
402 => 'Payment Required',
|
||||
403 => 'Forbidden',
|
||||
404 => 'Not Found',
|
||||
405 => 'Method Not Allowed',
|
||||
406 => 'Not Acceptable',
|
||||
407 => 'Proxy Authentication Required',
|
||||
408 => 'Request Timeout',
|
||||
409 => 'Conflict',
|
||||
410 => 'Gone',
|
||||
411 => 'Length Required',
|
||||
412 => 'Precondition Failed',
|
||||
413 => 'Request Entity Too Large',
|
||||
414 => 'Request-URI Too Long',
|
||||
415 => 'Unsupported Media Type',
|
||||
416 => 'Requested Range Not Satisfiable',
|
||||
417 => 'Expectation Failed',
|
||||
422 => 'Unprocessable Entity',
|
||||
423 => 'Locked',
|
||||
424 => 'Failed Dependency',
|
||||
425 => 'Reserved for WebDAV advanced collections expired proposal',
|
||||
426 => 'Upgrade required',
|
||||
428 => 'Precondition Required',
|
||||
429 => 'Too Many Requests',
|
||||
431 => 'Request Header Fields Too Large',
|
||||
500 => 'Internal Server Error',
|
||||
501 => 'Not Implemented',
|
||||
502 => 'Bad Gateway',
|
||||
503 => 'Service Unavailable',
|
||||
504 => 'Gateway Timeout',
|
||||
505 => 'HTTP Version Not Supported',
|
||||
506 => 'Variant Also Negotiates (Experimental)',
|
||||
507 => 'Insufficient Storage',
|
||||
508 => 'Loop Detected',
|
||||
510 => 'Not Extended',
|
||||
511 => 'Network Authentication Required',
|
||||
);
|
||||
|
||||
/**
|
||||
* @param int $code 状态码
|
||||
* @param double $duration 请求时长
|
||||
* @param array $headers 响应头部
|
||||
* @param string $body 响应内容
|
||||
* @param string $error 错误描述
|
||||
*/
|
||||
public function __construct($code, $duration, array $headers = array(), $body = null, $error = null)
|
||||
{
|
||||
$this->statusCode = $code;
|
||||
$this->duration = $duration;
|
||||
$this->headers = $headers;
|
||||
$this->body = $body;
|
||||
$this->error = $error;
|
||||
$this->jsonData = null;
|
||||
if ($error !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($body === null) {
|
||||
if ($code >= 400) {
|
||||
$this->error = self::$statusTexts[$code];
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (self::isJson($headers)) {
|
||||
try {
|
||||
$jsonData = self::bodyJson($body);
|
||||
if ($code >= 400) {
|
||||
$this->error = $body;
|
||||
if ($jsonData['error'] !== null) {
|
||||
$this->error = $jsonData['error'];
|
||||
}
|
||||
}
|
||||
$this->jsonData = $jsonData;
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error = $body;
|
||||
if ($code >= 200 && $code < 300) {
|
||||
$this->error = $e->getMessage();
|
||||
}
|
||||
}
|
||||
} elseif ($code >= 400) {
|
||||
$this->error = $body;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public function json()
|
||||
{
|
||||
return $this->jsonData;
|
||||
}
|
||||
|
||||
private static function bodyJson($body)
|
||||
{
|
||||
return \Qiniu\json_decode((string) $body, true, 512);
|
||||
}
|
||||
|
||||
public function xVia()
|
||||
{
|
||||
$via = $this->headers['X-Via'];
|
||||
if ($via === null) {
|
||||
$via = $this->headers['X-Px'];
|
||||
}
|
||||
if ($via === null) {
|
||||
$via = $this->headers['Fw-Via'];
|
||||
}
|
||||
return $via;
|
||||
}
|
||||
|
||||
public function xLog()
|
||||
{
|
||||
return $this->headers['X-Log'];
|
||||
}
|
||||
|
||||
public function xReqId()
|
||||
{
|
||||
return $this->headers['X-Reqid'];
|
||||
}
|
||||
|
||||
public function ok()
|
||||
{
|
||||
return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error === null;
|
||||
}
|
||||
|
||||
public function needRetry()
|
||||
{
|
||||
$code = $this->statusCode;
|
||||
if ($code < 0 || ($code / 100 === 5 and $code !== 579) || $code === 996) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static function isJson($headers)
|
||||
{
|
||||
return array_key_exists('content-type', $headers) || array_key_exists('Content-Type', $headers) &&
|
||||
strpos($headers['Content-Type'], 'application/json') === 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user