@@ -206,6 +206,83 @@ public function doRequest($method, $pathinfo, $jsondata = '', $verify = false, $
206
206
return $ isjson ? json_decode ($ content , true ) : $ content ;
207
207
}
208
208
209
+ /**
210
+ * 模拟发起上传请求
211
+ * @param string $pathinfo 请求路由
212
+ * @param string $filename 文件本地路径
213
+ * @param boolean $verify 是否验证
214
+ * @param boolean $isjson 返回JSON
215
+ * @return array|string
216
+ * @throws \WeChat\Exceptions\InvalidResponseException
217
+ */
218
+ public function doUpload ($ pathinfo , $ filename , $ verify = false , $ isjson = true )
219
+ {
220
+ $ filedata = file_get_contents ($ filename );
221
+ $ fileinfo = [
222
+ 'sha256 ' => hash ("sha256 " , $ filedata ),
223
+ 'filename ' => basename ($ filename )
224
+ ];
225
+ $ jsondata = json_encode ($ fileinfo );
226
+ list ($ time , $ nonce ) = [time (), uniqid () . rand (1000 , 9999 )];
227
+ $ signstr = join ("\n" , ['POST ' , $ pathinfo , $ time , $ nonce , $ jsondata , '' ]);
228
+ // 生成签名
229
+ $ sign = $ this ->signBuild ($ signstr );
230
+ // 生成数据签名TOKEN
231
+ $ token = sprintf ('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s" ' ,
232
+ $ this ->config ['mch_id ' ], $ nonce , $ time , $ this ->config ['cert_serial ' ], $ sign
233
+ );
234
+ $ location = (preg_match ('|^https?://| ' , $ pathinfo ) ? '' : $ this ->base ) . $ pathinfo ;
235
+ $ boundary = mt_rand (100000000000000000 , 999999999999999999 );
236
+ $ header = [
237
+ 'Accept: application/json ' ,
238
+ "Content-Type: multipart/form-data; boundary= {$ boundary }" ,
239
+ 'User-Agent: https://thinkadmin.top ' ,
240
+ "Authorization: WECHATPAY2-SHA256-RSA2048 {$ token }" ,
241
+ "serial_no: {$ this ->config ['mp_cert_serial ' ]}" ,
242
+ "nonce_str: {$ nonce }" ,
243
+ "signature: {$ sign }"
244
+ ];
245
+ $ lines = [];
246
+ $ line [] = "-- {$ boundary }" ;
247
+ $ line [] = "Content-Disposition: form-data; name= \"meta \"" ;
248
+ $ line [] = "Content-Type: application/json " ;
249
+ $ line [] = "" ;
250
+ $ line [] = $ jsondata ;
251
+ $ line [] = "-- {$ boundary }" ;
252
+ $ line [] = "Content-Disposition: form-data; name= \"file \"; filename= \"{$ fileinfo ['filename ' ]}\"; " ;
253
+ $ line [] = "Content-Type: image/jpg " ;
254
+ $ line [] = "" ;
255
+ $ line [] = $ filedata ;
256
+ $ line [] = "-- {$ boundary }-- " ;
257
+ $ postdata = join ("\r\n" , $ line );
258
+ list ($ header , $ content ) = $ this ->_doRequestCurl ('POST ' , $ location , [
259
+ 'data ' => $ postdata , 'header ' => $ header ,
260
+ ]);
261
+ if ($ verify ) {
262
+ $ headers = [];
263
+ foreach (explode ("\n" , $ header ) as $ line ) {
264
+ if (stripos ($ line , 'Wechatpay ' ) !== false ) {
265
+ list ($ name , $ value ) = explode (': ' , $ line );
266
+ list (, $ keys ) = explode ('wechatpay- ' , strtolower ($ name ));
267
+ $ headers [$ keys ] = trim ($ value );
268
+ }
269
+ }
270
+ try {
271
+ if (empty ($ headers )) {
272
+ return $ isjson ? json_decode ($ content , true ) : $ content ;
273
+ }
274
+ $ string = join ("\n" , [$ headers ['timestamp ' ], $ headers ['nonce ' ], $ content , '' ]);
275
+ if (!$ this ->signVerify ($ string , $ headers ['signature ' ], $ headers ['serial ' ])) {
276
+ throw new InvalidResponseException ('验证响应签名失败 ' );
277
+ }
278
+ } catch (\Exception $ exception ) {
279
+ throw new InvalidResponseException ($ exception ->getMessage (), $ exception ->getCode ());
280
+ }
281
+ }
282
+ return $ isjson ? json_decode ($ content , true ) : $ content ;
283
+ }
284
+
285
+
209
286
/**
210
287
* 通过CURL模拟网络请求
211
288
* @param string $method 请求方法
0 commit comments