88class Qiniu_Error
99{
1010 public $ Err ; // string
11- public $ Reqid ; // string
11+ public $ Reqid ; // string
1212 public $ Details ; // []string
1313 public $ Code ; // int
1414
@@ -48,7 +48,7 @@ class Qiniu_Response
4848
4949 public function __construct ($ code , $ body )
5050 {
51- $ this ->StatusCode = code;
51+ $ this ->StatusCode = $ code ;
5252 $ this ->Header = array ();
5353 $ this ->Body = $ body ;
5454 $ this ->ContentLength = strlen ($ body );
@@ -62,7 +62,7 @@ function Qiniu_Header_Get($header, $key) // => $val
6262{
6363 $ val = @$ header [$ key ];
6464 if (isset ($ val )) {
65- if (isarray ($ val )) {
65+ if (is_array ($ val )) {
6666 return $ val [0 ];
6767 }
6868 return $ val ;
@@ -117,10 +117,18 @@ function Qiniu_Client_do($req) // => ($resp, $error)
117117 CURLOPT_CUSTOMREQUEST => 'POST ' ,
118118 CURLOPT_URL => $ url ['path ' ]
119119 );
120+ $ httpHeader = $ req ->Header ;
121+ if (!empty ($ httpHeader ))
122+ {
123+ $ header = array ();
124+ foreach ($ httpHeader as $ key => $ parsedUrlValue ) {
125+ $ header [] = "$ key: $ parsedUrlValue " ;
126+ }
127+ $ options [CURLOPT_HTTPHEADER ] = $ header ;
128+ }
120129 $ body = $ req ->Body ;
121130 if (!empty ($ body )) {
122131 $ options [CURLOPT_POSTFIELDS ] = $ body ;
123- $ options [CURLOPT_POSTFIELDSIZE ] = strlen ($ body );
124132 }
125133 curl_setopt_array ($ ch , $ options );
126134 $ result = curl_exec ($ ch );
@@ -138,19 +146,27 @@ function Qiniu_Client_do($req) // => ($resp, $error)
138146 return array ($ resp , null );
139147}
140148
141- class Qiniu_Client
149+ class Qiniu_HttpClient
150+ {
151+ public function RoundTrip ($ req ) // => ($resp, $error)
152+ {
153+ return Qiniu_Client_do ($ req );
154+ }
155+ }
156+
157+ class Qiniu_MacHttpClient
142158{
143- private $ mac ;
159+ public $ Mac ;
144160
145161 public function __construct ($ mac )
146162 {
147- $ this ->mac = Qiniu_RequireMac ($ mac );
163+ $ this ->Mac = Qiniu_RequireMac ($ mac );
148164 }
149165
150- public function Exec ($ req ) // => ($resp, $error)
166+ public function RoundTrip ($ req ) // => ($resp, $error)
151167 {
152168 $ incbody = Qiniu_Client_incBody ($ req );
153- $ token = $ this ->mac ->SignRequest ($ req , $ incbody );
169+ $ token = $ this ->Mac ->SignRequest ($ req , $ incbody );
154170 $ req ->Header ['Authorization ' ] = "QBox $ token " ;
155171 return Qiniu_Client_do ($ req );
156172 }
@@ -161,6 +177,7 @@ public function Exec($req) // => ($resp, $error)
161177function Qiniu_Client_ret ($ resp ) // => ($data, $error)
162178{
163179 $ code = $ resp ->StatusCode ;
180+ $ data = null ;
164181 if ($ code >= 200 && $ code <= 299 ) {
165182 if ($ resp ->ContentLength !== 0 ) {
166183 $ data = json_decode ($ resp ->Body , true );
@@ -180,7 +197,7 @@ function Qiniu_Client_Call($self, $url) // => ($data, $error)
180197{
181198 $ u = array ('path ' => $ url );
182199 $ req = new Qiniu_Request ($ u , null );
183- list ($ resp , $ err ) = $ self ->Exec ($ req );
200+ list ($ resp , $ err ) = $ self ->RoundTrip ($ req );
184201 if ($ err !== null ) {
185202 return array (null , $ err );
186203 }
@@ -191,7 +208,7 @@ function Qiniu_Client_CallNoRet($self, $url) // => $error
191208{
192209 $ u = array ('path ' => $ url );
193210 $ req = new Qiniu_Request ($ u , null );
194- list ($ resp , $ err ) = $ self ->Exec ($ req );
211+ list ($ resp , $ err ) = $ self ->RoundTrip ($ req );
195212 if ($ err !== null ) {
196213 return array (null , $ err );
197214 }
@@ -201,5 +218,62 @@ function Qiniu_Client_CallNoRet($self, $url) // => $error
201218 return Qiniu_ResponseError ($ resp );
202219}
203220
221+ function Qiniu_Client_CallWithForm (
222+ $ self , $ url , $ params , $ contentType = 'application/x-www-form-urlencoded ' ) // => ($data, $error)
223+ {
224+ $ u = array ('path ' => $ url );
225+ if ($ contentType === 'application/x-www-form-urlencoded ' ) {
226+ if (is_array ($ params )) {
227+ $ params = http_build_query ($ params );
228+ }
229+ }
230+ $ req = new Qiniu_Request ($ u , $ params );
231+ if ($ contentType !== 'multipart/form-data ' ) {
232+ $ req ->Header ['Content-Type ' ] = $ contentType ;
233+ }
234+ list ($ resp , $ err ) = $ self ->RoundTrip ($ req );
235+ if ($ err !== null ) {
236+ return array (null , $ err );
237+ }
238+ return Qiniu_Client_ret ($ resp );
239+ }
240+
241+ // --------------------------------------------------------------------------------
242+
243+ function Qiniu_Client_CallWithMultipartForm ($ self , $ url , $ fields , $ files )
244+ {
245+ list ($ contentType , $ body ) = Qiniu_Build_MultipartForm ($ fields , $ files );
246+ return Qiniu_Client_CallWithForm ($ self , $ url , $ body , $ contentType );
247+ }
248+
249+ function Qiniu_Build_MultipartForm ($ fields , $ files ) // => ($contentType, $body)
250+ {
251+ $ data = array ();
252+ $ mimeBoundary = md5 (microtime ());
253+
254+ foreach ($ fields as $ name => $ val ){
255+ array_push ($ data , '-- ' . $ mimeBoundary );
256+ array_push ($ data , "Content-Disposition: form-data; name= $ name " );
257+ array_push ($ data , '' );
258+ array_push ($ data , $ val );
259+ }
260+
261+ foreach ($ files as $ file ) {
262+ array_push ($ data , '-- ' . $ mimeBoundary );
263+ list ($ name , $ fileName , $ fileCxt ) = $ file ;
264+ array_push ($ data , "Content-Disposition: form-data; name= $ name; filename= $ fileName " );
265+ array_push ($ data , 'Content-Type: application/octet-stream ' );
266+ array_push ($ data , '' );
267+ array_push ($ data , $ fileCxt );
268+ }
269+
270+ array_push ($ data , '-- ' . $ mimeBoundary );
271+ array_push ($ data , '' );
272+
273+ $ body = implode ("\r\n" , $ data );
274+ $ contentType = 'multipart/form-data; boundary= ' . $ mimeBoundary ;
275+ return array ($ contentType , $ body );
276+ }
277+
204278// --------------------------------------------------------------------------------
205279
0 commit comments