|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once("auth_digest.php"); |
| 4 | + |
| 5 | +// -------------------------------------------------------------------------------- |
| 6 | +// class Qiniu_Error |
| 7 | + |
| 8 | +class Qiniu_Error |
| 9 | +{ |
| 10 | + public $Err; // string |
| 11 | + public $Reqid; // string |
| 12 | + public $Details; // []string |
| 13 | + public $Code; // int |
| 14 | + |
| 15 | + public function __construct($code, $err) |
| 16 | + { |
| 17 | + $this->Code = $code; |
| 18 | + $this->Err = $err; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// -------------------------------------------------------------------------------- |
| 23 | +// class Qiniu_Request |
| 24 | + |
| 25 | +class Qiniu_Request |
| 26 | +{ |
| 27 | + public $URL; |
| 28 | + public $Header; |
| 29 | + public $Body; |
| 30 | + |
| 31 | + public function __construct($url, $body) |
| 32 | + { |
| 33 | + $this->URL = $url; |
| 34 | + $this->Header = array(); |
| 35 | + $this->Body = $body; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// -------------------------------------------------------------------------------- |
| 40 | +// class Qiniu_Response |
| 41 | + |
| 42 | +class Qiniu_Response |
| 43 | +{ |
| 44 | + public $StatusCode; |
| 45 | + public $Header; |
| 46 | + public $ContentLength; |
| 47 | + public $Body; |
| 48 | + |
| 49 | + public function __construct($code, $body) |
| 50 | + { |
| 51 | + $this->StatusCode = code; |
| 52 | + $this->Header = array(); |
| 53 | + $this->Body = $body; |
| 54 | + $this->ContentLength = strlen($body); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// -------------------------------------------------------------------------------- |
| 59 | +// class Qiniu_Header |
| 60 | + |
| 61 | +function Qiniu_Header_Get($header, $key) // => $val |
| 62 | +{ |
| 63 | + $val = @$header[$key]; |
| 64 | + if (isset($val)) { |
| 65 | + if (isarray($val)) { |
| 66 | + return $val[0]; |
| 67 | + } |
| 68 | + return $val; |
| 69 | + } else { |
| 70 | + return ''; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function Qiniu_ResponseError($resp) // => $error |
| 75 | +{ |
| 76 | + $header = $resp->Header; |
| 77 | + $details = Qiniu_Header_Get($header, 'X-Log'); |
| 78 | + $reqId = Qiniu_Header_Get($header, 'X-Reqid'); |
| 79 | + $err = new Qiniu_Error($resp->StatusCode, null); |
| 80 | + |
| 81 | + if ($err->Code > 299) { |
| 82 | + if ($resp->ContentLength !== 0) { |
| 83 | + if (Qiniu_Header_Get($header, 'Content-Type') === 'application/json') { |
| 84 | + $ret = json_decode($resp->Body, true); |
| 85 | + $err->Err = $ret['error']; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return $err; |
| 90 | +} |
| 91 | + |
| 92 | +// -------------------------------------------------------------------------------- |
| 93 | +// class Qiniu_Client |
| 94 | + |
| 95 | +function Qiniu_Client_incBody($req) // => $incbody |
| 96 | +{ |
| 97 | + $body = $req->Body; |
| 98 | + if (!isset($body)) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + $ct = Qiniu_Header_Get($req->Header, 'Content-Type'); |
| 103 | + if ($ct === 'application/x-www-form-urlencoded') { |
| 104 | + return true; |
| 105 | + } |
| 106 | + return false; |
| 107 | +} |
| 108 | + |
| 109 | +function Qiniu_Client_do($req) // => ($resp, $error) |
| 110 | +{ |
| 111 | + $ch = curl_init(); |
| 112 | + $url = $req->URL; |
| 113 | + $options = array( |
| 114 | + CURLOPT_RETURNTRANSFER => true, |
| 115 | + CURLOPT_SSL_VERIFYPEER => false, |
| 116 | + CURLOPT_SSL_VERIFYHOST => false, |
| 117 | + CURLOPT_CUSTOMREQUEST => 'POST', |
| 118 | + CURLOPT_URL => $url['path'] |
| 119 | + ); |
| 120 | + $body = $req->Body; |
| 121 | + if (!empty($body)) { |
| 122 | + $options[CURLOPT_POSTFIELDS] = $body; |
| 123 | + $options[CURLOPT_POSTFIELDSIZE] = strlen($body); |
| 124 | + } |
| 125 | + curl_setopt_array($ch, $options); |
| 126 | + $result = curl_exec($ch); |
| 127 | + $ret = curl_errno($ch); |
| 128 | + if ($ret !== 0) { |
| 129 | + $err = new Qiniu_Error(0, curl_error($ch)); |
| 130 | + curl_close($ch); |
| 131 | + return array(null, $err); |
| 132 | + } |
| 133 | + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 134 | + $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
| 135 | + curl_close($ch); |
| 136 | + $resp = new Qiniu_Response($code, $result); |
| 137 | + $resp->Header['Content-Type'] = $contentType; |
| 138 | + return array($resp, null); |
| 139 | +} |
| 140 | + |
| 141 | +class Qiniu_Client |
| 142 | +{ |
| 143 | + private $mac; |
| 144 | + |
| 145 | + public function __construct($mac) |
| 146 | + { |
| 147 | + $this->mac = Qiniu_RequireMac($mac); |
| 148 | + } |
| 149 | + |
| 150 | + public function Exec($req) // => ($resp, $error) |
| 151 | + { |
| 152 | + $incbody = Qiniu_Client_incBody($req); |
| 153 | + $token = $this->mac->SignRequest($req, $incbody); |
| 154 | + $req->Header['Authorization'] = "QBox $token"; |
| 155 | + return Qiniu_Client_do($req); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// -------------------------------------------------------------------------------- |
| 160 | + |
| 161 | +function Qiniu_Client_ret($resp) // => ($data, $error) |
| 162 | +{ |
| 163 | + $code = $resp->StatusCode; |
| 164 | + if ($code >= 200 && $code <= 299) { |
| 165 | + if ($resp->ContentLength !== 0) { |
| 166 | + $data = json_decode($resp->Body, true); |
| 167 | + if ($data === null) { |
| 168 | + $err = new Qiniu_Error(0, json_last_error_msg()); |
| 169 | + return array(null, $err); |
| 170 | + } |
| 171 | + } |
| 172 | + if ($code === 200) { |
| 173 | + return array($data, null); |
| 174 | + } |
| 175 | + } |
| 176 | + return array($data, Qiniu_ResponseError($resp)); |
| 177 | +} |
| 178 | + |
| 179 | +function Qiniu_Client_Call($self, $url) // => ($data, $error) |
| 180 | +{ |
| 181 | + $u = array('path' => $url); |
| 182 | + $req = new Qiniu_Request($u, null); |
| 183 | + list($resp, $err) = $self->Exec($req); |
| 184 | + if ($err !== null) { |
| 185 | + return array(null, $err); |
| 186 | + } |
| 187 | + return Qiniu_Client_ret($resp); |
| 188 | +} |
| 189 | + |
| 190 | +function Qiniu_Client_CallNoRet($self, $url) // => $error |
| 191 | +{ |
| 192 | + $u = array('path' => $url); |
| 193 | + $req = new Qiniu_Request($u, null); |
| 194 | + list($resp, $err) = $self->Exec($req); |
| 195 | + if ($err !== null) { |
| 196 | + return array(null, $err); |
| 197 | + } |
| 198 | + if ($resp->StatusCode === 200) { |
| 199 | + return null; |
| 200 | + } |
| 201 | + return Qiniu_ResponseError($resp); |
| 202 | +} |
| 203 | + |
| 204 | +// -------------------------------------------------------------------------------- |
| 205 | + |
0 commit comments