MiniSubController.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Imports\ExpressImport;
  4. use App\Models\Order;
  5. use App\Models\Region;
  6. use App\Models\Store;
  7. use App\Models\User;
  8. use App\Models\Bank;
  9. use App\Models\Bank123;
  10. use Excel;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Cache;
  15. class MiniSubController extends Controller
  16. {
  17. //申请小微商户
  18. public function apply_wx()
  19. {
  20. $url = "https://api.mch.weixin.qq.com/v3/ecommerce/applyments/";//这个是微信的申请地址
  21. $param = array(
  22. "out_request_no" => "20200708093101909876",//申请编号,用户自定义就可以
  23. "organization_type" => "2401",//申请类型 2401代表申请的是小微商户(就是个人,没有营业执照的人),其它类型可参考微信v3文档
  24. "need_account_info" => false,//是否填写结算银行账户,提现会提到这个账户上,设置true需要添加银行卡信息等。这里我就设置false了。因为绑定银行卡可以通过微信中的小程序“商家助手”自己去绑定。也可以通过微信的绑卡接口在写一个绑定银行卡的功能。
  25. "account_info" => [
  26. "bank_account_type" => "74",
  27. "account_name" => "",//开户名称
  28. "account_bank" => "工商银行",//开户银行
  29. "bank_address_code" => "110000",//开户银行省市编码
  30. "bank_branch_id" => "402713354941",
  31. "bank_name" => "",//开户银行全称(含支行)
  32. "account_number" => "",//银行账户
  33. ],
  34. "contact_info" => array(//填写一个店铺的管理员信息
  35. "contact_type" => "65",//管理员类型 65是经营者/法人 其它类型看微信文档
  36. "contact_name" => $this->getEncrypts("张奇"),//管理员姓名getEncrypts是加密字符串的方法,见下文。
  37. "mobile_phone" => $this->getEncrypts("15236877164"),//管理员电话getEncrypts是加密字符串的方法
  38. "contact_id_card_number" => $this->getEncrypts("412326199612220311"),//管理员身份证号码
  39. // "contact_email" =>$this->getEncrypts("邮箱@.com"),//管理员的邮箱地址
  40. ),
  41. "sales_scene_info" => array(//店铺信息
  42. "store_name" => "大卫博士微商店",//店铺全称
  43. ),
  44. "merchant_shortname" => "大卫博士",//商户简称
  45. "id_card_info" => array(//经营者/法人的身份证信息
  46. 'id_card_copy' => '9DNPQr2pl-BoHpWimJBY76nt_fEehBjGAT6PTijhvHRMfWN8COZtSb_LTgU8159D04a-4QzYCGPl0urT8jl3-YfZ-W_3BFOVBEKXe1TCb6Y', // 身份证人像面照片
  47. 'id_card_national' => '9DNPQr2pl-BoHpWimJBY7xVvut4fNtB2n4GVjEgEvr_DsJL43easTmbP6Ofp4Wu0O2fzm1qWstPBIZ57QDwa69oHp_Gnc2uivCZegMtFC5U', // 身份证国徽面照片
  48. "id_card_name" => $this->getEncrypts('张奇'),//身份证上的姓名 getEncrypts()字符串加密方法 见下文
  49. "id_card_number" => $this->getEncrypts("412326199612220311"),//身份证号,此身份证号可以和管理员是一个人
  50. "id_card_valid_time" => "2027-09-07" //身份证国徽面的到期日期
  51. )
  52. );
  53. $merchant_id = "1604585534"; //服务商的商户号
  54. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//商户证书的序列号,可在微信商户平台API安全 中获取
  55. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  56. $timestamp = time();//时间戳
  57. $nonce = $this->nonce_str();//获取随机字符串
  58. $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($param), $mch_private_key, $merchant_id, $serial_no);//进行签名操作
  59. $header = [//设置发送的头信息
  60. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,//签名
  61. 'Accept:application/json',
  62. 'User-Agent:' . $merchant_id,//服务商的商户号
  63. 'Content-Type:application/json',
  64. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书的序列号,注意:是平台证书,不是上文的商户证书序列号
  65. ];
  66. $result = $this->curl($url, json_encode($param), $header);//curl方法见下文
  67. $result = json_decode($result, true);
  68. return $result;
  69. var_dump($result);
  70. // $result['applyment_id'] 返回了 说明申请成功了
  71. }
  72. //查询进件状态
  73. public function query()
  74. {
  75. $out_request_no = '20200708093101909876';//申请编号
  76. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/applyments/out-request-no/' . $out_request_no;//查询地址
  77. $merchant_id = "1604585534";//商户号
  78. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  79. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  80. $timestamp = time();//时间戳
  81. $nonce = $this->nonce_str();//随机字符串
  82. $body = "";
  83. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  84. $header = [
  85. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  86. 'Accept:application/json',
  87. 'User-Agent:' . $merchant_id,
  88. 'Content-Type:application/json',
  89. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  90. ];
  91. $result = $this->curl($url, '', $header, 'GET');
  92. $result = json_decode($result, true);
  93. var_dump($result);
  94. $result['applyment_state'] == "FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
  95. }
  96. //加密敏感字符串
  97. public function getEncrypts($str = "张三")
  98. {
  99. // $public_key_path = 'D:\phpstudy_pro\WWW\weidian-api/cert/cert.pem';//获取的文件临时保存到服务器
  100. $public_key_path='/www/wwwroot/weidian.jiuweiyun.cn/weidian-online-api/cert/cert.pem';//这是获取平台证书临时存起来的平台公钥,通过getzhengshu()方法获取,见下文。。。注意是平台证书,不是商户证书
  101. $public_key = file_get_contents($public_key_path);
  102. $encrypted = '';
  103. if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) {
  104. //base64编码
  105. $sign = base64_encode($encrypted);
  106. } else {
  107. throw new Exception('encrypt failed');
  108. }
  109. return $sign;
  110. // if(openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)){
  111. // $sign=base64_encode($encrypted);
  112. // }else{
  113. // echo "error";exit;
  114. // }
  115. // return $sign;//返回加密的敏感字符串
  116. }
  117. //上传证件图片
  118. public function uploadmedia($path = "uploads/header/z.jpg")
  119. {
  120. $url = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";//微信的上传地址
  121. $merchant_id = "1604585534";//商户号
  122. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//商户证书序列号,注意是商户证书,不是平台证书序列号
  123. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法
  124. $timestamp = time();//时间戳
  125. $nonce = $this->nonce_str();//随机字符串
  126. $fi = new \finfo(FILEINFO_MIME_TYPE);
  127. $mime_type = $fi->file($path);
  128. $meta = [
  129. 'filename' => pathinfo($path, 2),
  130. 'sha256' => hash_file('sha256', $path)
  131. ];
  132. $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($meta), $mch_private_key, $merchant_id, $serial_no);
  133. $boundary = uniqid();
  134. $header = [
  135. 'Content-Type:multipart/form-data;boundary=' . $boundary,
  136. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  137. 'Accept:application/json',
  138. 'User-Agent:' . $merchant_id
  139. ];
  140. $body = '--' . $boundary . "\r\n";
  141. $body .= 'Content-Disposition:form-data; name="meta"' . "\r\n";
  142. $body .= 'Content-Type:application/json' . "\r\n\r\n";
  143. $body .= json_encode($meta) . "\r\n";
  144. $body .= '--' . $boundary . "\r\n";
  145. $body .= 'Content-Disposition:form-data;name="file";filename="' . $meta['filename'] . '"' . "\r\n";
  146. $body .= 'Content-Type:' . $mime_type . "\r\n\r\n";
  147. $body .= file_get_contents($path) . "\r\n";
  148. $body .= '--' . $boundary . '--' . "\r\n";
  149. $result = $this->curl($url, $body, $header);
  150. $result = json_decode($result, true);
  151. return $result['media_id'];//返回微信返回来的图片标识
  152. }
  153. //读取商户api证书公钥
  154. public function getPublicKey()
  155. {
  156. return openssl_get_privatekey(file_get_contents(config('wechat.payment.default.key_path')));//商户平台API安全证书中下载,保存到服务器
  157. }
  158. //生成随机字符串
  159. public function nonce_str($length = 32)
  160. {
  161. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  162. $str = "";
  163. for ($i = 0; $i < $length; $i++) {
  164. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  165. }
  166. return $str;
  167. }
  168. //签名
  169. public function sign($url, $http_method, $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no)
  170. {
  171. $url_parts = parse_url($url);
  172. $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
  173. $message =
  174. $http_method . "\n" .
  175. $canonical_url . "\n" .
  176. $timestamp . "\n" .
  177. $nonce . "\n" .
  178. $body . "\n";
  179. openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
  180. $sign = base64_encode($raw_sign);
  181. $schema = 'WECHATPAY2-SHA256-RSA2048';
  182. $token = sprintf(
  183. 'mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
  184. $merchant_id,
  185. $nonce,
  186. $sign,
  187. $timestamp,
  188. $serial_no
  189. );
  190. return $token;
  191. }
  192. //curl提交
  193. public function curl($url, $data = [], $header, $method = 'POST')
  194. {
  195. $curl = curl_init();
  196. curl_setopt($curl, CURLOPT_URL, $url);
  197. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  198. curl_setopt($curl, CURLOPT_HEADER, false);
  199. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  200. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  201. if ($method == "POST") {
  202. curl_setopt($curl, CURLOPT_POST, TRUE);
  203. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  204. }
  205. $result = curl_exec($curl);
  206. curl_close($curl);
  207. return $result;
  208. }
  209. //获取平台证书
  210. public function getzhengshu()
  211. {
  212. $url = "https://api.mch.weixin.qq.com/v3/certificates";//获取地址
  213. $timestamp = time();//时间戳
  214. $nonce = $this->nonce_str();//获取一个随机字符串
  215. $body = "";
  216. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥
  217. $merchant_id = config('wechat.payment.default.mch_id');//商户号
  218. $serial_no = '696B623841134292AAB48D4EC5907FB376504E98';//商户证书序列号
  219. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);
  220. $header = [
  221. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  222. 'Accept:application/json',
  223. 'User-Agent:' . $merchant_id
  224. ];
  225. $result = $this->curl($url, '', $header, 'GET');
  226. $result = json_decode($result, true);
  227. // Log::error($result);
  228. $serial_no = $result['data'][0]['serial_no'];//获取的平台证书序列号
  229. $encrypt_certificate = $result['data'][0]['encrypt_certificate'];
  230. $sign_key = config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  231. $result = $this->decryptToString($encrypt_certificate['associated_data'], $encrypt_certificate['nonce'], $encrypt_certificate['ciphertext'], $sign_key);
  232. file_put_contents('/www/wwwroot/weidian.jiuweiyun.cn/weidian-online-api/cert/cert.pem', $result);//获取的文件临时保存到服务器
  233. return $serial_no;//返回平台证书序列号
  234. }
  235. //解密返回的信息
  236. public function decryptToString($associatedData, $nonceStr, $ciphertext, $aesKey)
  237. {
  238. if (strlen($aesKey) != 32) {
  239. throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
  240. }
  241. // $this->aesKey = $aesKey;
  242. $ciphertext = \base64_decode($ciphertext);
  243. if (strlen($ciphertext) <= 16) {
  244. return false;
  245. }
  246. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
  247. \sodium_crypto_aead_aes256gcm_is_available()) {
  248. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  249. return '1111';
  250. }
  251. // ext-libsodium (need install libsodium-php 1.x via pecl)
  252. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
  253. \Sodium\crypto_aead_aes256gcm_is_available()) {
  254. return '222';
  255. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  256. }
  257. // if(function_exists('\sodium_crypto_aead_aes256gcm_is_available')&& \sodium_crypto_aead_aes256gcm_is_available()){
  258. // return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext,$associatedData,$nonceStr,$aesKey);
  259. // }
  260. if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
  261. $ctext = substr($ciphertext, 0, -16);
  262. $authTag = substr($ciphertext, -16);
  263. return \openssl_decrypt(
  264. $ctext,
  265. 'aes-256-gcm',
  266. $aesKey,
  267. \OPENSSL_RAW_DATA,
  268. $nonceStr,
  269. $authTag,
  270. $associatedData
  271. );
  272. }
  273. throw new \RuntimeException('php7.1');
  274. }
  275. //完结分账信息
  276. public function finish(Request $request)
  277. {
  278. $order_no = $request->input('order_no');
  279. $order = Order::where('order_no', $order_no)->first();
  280. $user = User::find(Auth::user()->id);
  281. // $sub_mchid=$request->input('sub_mchid');
  282. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/finish-order';//地址
  283. $merchant_id = config('wechat.payment.default.mch_id');//商户号
  284. $serial_no = config('wechat.payment.default.serial_no');//不是平台证书序列号
  285. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  286. $timestamp = time();//时间戳
  287. $nonce = $this->nonce_str();//随机字符串
  288. $data = [
  289. 'sub_mchid' => Store::where('id', $user->store_id)->value('sub_mchid'),
  290. 'transaction_id' => $order->wechat_pay_no,//微信支付单号
  291. 'out_order_no' => 'finish' . '_' . $order->order_no,//订单号
  292. 'description' => '分账完结',
  293. ];
  294. $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
  295. $header = [
  296. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  297. 'Accept:application/json',
  298. 'User-Agent:' . $merchant_id,
  299. 'Content-Type:application/json',
  300. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  301. ];
  302. $result = $this->curl($url, json_encode($data), $header);
  303. $result = json_decode($result, true);
  304. return $this->success($result);
  305. }
  306. //查询分账
  307. public function fenzhang()
  308. {
  309. $sub_mchid = '1604640016';
  310. $transaction_id = '4200000902202104094861688627';
  311. $out_order_no = 'WJ21040906580535143167060';
  312. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/orders?sub_mchid=' . $sub_mchid . '&transaction_id=' . $transaction_id . '&out_order_no=' . $out_order_no;//查询地址
  313. $merchant_id = "1604585534";//商户号
  314. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  315. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  316. $timestamp = time();//时间戳
  317. $nonce = $this->nonce_str();//随机字符串
  318. $body = "";
  319. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  320. $header = [
  321. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  322. 'Accept:application/json',
  323. 'User-Agent:' . $merchant_id,
  324. 'Content-Type:application/json',
  325. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  326. ];
  327. $result = $this->curl($url, '', $header, 'GET');
  328. $result = json_decode($result, true);
  329. return $this->success($result);
  330. // var_dump($result);exit;
  331. }
  332. public function fenzhangyue()
  333. {
  334. // $sub_mchid='1604640016';
  335. $transaction_id = '4200000838202012077922886767';//'4200000753202012077136468639';
  336. // $out_order_no='finish_DD20120716581049363773415';
  337. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/orders/' . $transaction_id . '/amounts';//查询地址
  338. $merchant_id = "1604585534";//商户号
  339. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  340. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  341. $timestamp = time();//时间戳
  342. $nonce = $this->nonce_str();//随机字符串
  343. $body = "";
  344. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  345. $header = [
  346. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  347. 'Accept:application/json',
  348. 'User-Agent:' . $merchant_id,
  349. 'Content-Type:application/json',
  350. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  351. ];
  352. $result = $this->curl($url, '', $header, 'GET');
  353. $result = json_decode($result, true);
  354. return $this->success($result);
  355. // var_dump($result);exit;
  356. }
  357. //查询实时余额
  358. public function shishiyue(Request $request)
  359. {
  360. $sub_mchid = $request->input('sub_mchid');//'1604640016';
  361. // $transaction_id='4200000838202012077922886767';//'4200000753202012077136468639';
  362. // $out_order_no='finish_DD20120716581049363773415';
  363. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/balance/' . $sub_mchid;//查询地址
  364. $merchant_id = "1604585534";//商户号
  365. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  366. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  367. $timestamp = time();//时间戳
  368. $nonce = $this->nonce_str();//随机字符串
  369. $body = "";
  370. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  371. $header = [
  372. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  373. 'Accept:application/json',
  374. 'User-Agent:' . $merchant_id,
  375. 'Content-Type:application/json',
  376. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  377. ];
  378. $result = $this->curl($url, '', $header, 'GET');
  379. $result = json_decode($result, true);
  380. return $this->success($result);
  381. // var_dump($result);exit;https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw
  382. }
  383. //二级商户提现余额
  384. public function tixianyue(Request $request)
  385. {
  386. $sub_mchid = $request->input('sub_mchid');
  387. $amount = $request->input('amount');
  388. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw';//查询地址
  389. $merchant_id = "1604585534";//商户号
  390. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  391. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  392. $timestamp = time();//时间戳
  393. $nonce = $this->nonce_str();//随机字符串
  394. $data = [
  395. 'sub_mchid' => $sub_mchid,
  396. 'out_request_no' => 'TX' . date('ymdHis') . substr(microtime(), 2, 6) . mt_rand(10000, 99999),
  397. 'amount' => (float)$amount,
  398. 'remark' => '二级商户提现',
  399. 'bank_memo' => '微信二级商户提现'
  400. ];
  401. $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
  402. $header = [
  403. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  404. 'Accept:application/json',
  405. 'User-Agent:' . $merchant_id,
  406. 'Content-Type:application/json',
  407. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  408. ];
  409. $result = $this->curl($url, json_encode($data), $header, 'POST');
  410. $result = json_decode($result, true);
  411. return $this->success($result);
  412. }
  413. //提现状态查询
  414. public function tixianchaxun()
  415. {
  416. $withdraw_id = '209000130152900202106022213780029';
  417. $sub_mchid = '1609479387';
  418. // $transaction_id='4200000838202012077922886767';//'4200000753202012077136468639';
  419. $out_order_no='TX21060220551298361934197';
  420. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/' . $out_order_no.'?sub_mchid='.$sub_mchid;//查询地址
  421. // $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/' . $withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
  422. $merchant_id = "1603992271";//商户号
  423. $serial_no = "696B623841134292AAB48D4EC5907FB376504E98";//不是平台证书序列号
  424. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  425. $timestamp = time();//时间戳
  426. $nonce = $this->nonce_str();//随机字符串
  427. $body = "";
  428. // $data = [
  429. // 'sub_mchid' => $sub_mchid,
  430. // ];
  431. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  432. $header = [
  433. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  434. 'Accept:application/json',
  435. 'User-Agent:' . $merchant_id,
  436. 'Content-Type:application/json',
  437. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  438. ];
  439. $result = $this->curl($url, '', $header, 'GET');
  440. // dd($result);
  441. $result = json_decode($result, true);
  442. return $this->success($result);
  443. // var_dump($result);exit;https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw
  444. }
  445. //退款接口
  446. public function tuikuan()
  447. {
  448. $sub_mchid = '1604640016';
  449. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//查询地址
  450. $merchant_id = "1604585534";//商户号
  451. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  452. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  453. $timestamp = time();//时间戳
  454. $nonce = $this->nonce_str();//随机字符串
  455. $data = [
  456. 'sub_mchid' => $sub_mchid,
  457. 'sp_appid' => config('wechat.payment.default.app_id'),
  458. 'out_trade_no' => 'DD20120716580942358726226',//'DD20120119281312383728170',
  459. 'out_refund_no' => 'TK' . date('ymdHis') . substr(microtime(), 2, 6) . mt_rand(10000, 99999),
  460. 'amount' => ["refund" => 1, "total" => 1, "currency" => "CNY"],
  461. 'notify_url' => 'http://weidian.jiuweiyun.cn/api/mini/refund_notify'
  462. ];
  463. // return $data;
  464. $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
  465. $header = [
  466. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  467. 'Accept:application/json',
  468. 'User-Agent:' . $merchant_id,
  469. 'Content-Type:application/json',
  470. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  471. ];
  472. $result = $this->curl($url, json_encode($data), $header, 'POST');
  473. $result = json_decode($result, true);
  474. return $this->success($result);
  475. }
  476. //退款通知
  477. public function refundNotify()
  478. {
  479. Log::info('退款通知123');
  480. defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
  481. //接收微信返回的数据数据,返回的xml格式
  482. $xmlData = file_get_contents('php://input');
  483. //将xml格式转换为数组
  484. Log::info($xmlData);
  485. $xmlData = json_decode($xmlData, true);
  486. $data = $xmlData['resource'];
  487. Log::info($data['associated_data']);
  488. $sign_key = config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  489. $result = $this->decryptToString($data['associated_data'], $data['nonce'], $data['ciphertext'], $sign_key);
  490. $result = json_decode($result, true);
  491. log::info($result['refund_status']);
  492. if ($result['refund_status'] == 'SUCCESS') {
  493. return true;
  494. }
  495. Log::info($result);
  496. return $this->success([], 'SUCCESS');
  497. //为了防止假数据,验证签名是否和返回的一样。
  498. //记录一下,返回回来的签名,生成签名的时候,必须剔除sign字段。
  499. // $sign = $data['sign'];
  500. // unset($data['sign']);
  501. // if($sign == self::getSign($data,config('wechat.payment.default.app_v3'))){
  502. // //签名验证成功后,判断返回微信返回的
  503. // if ($data['result_code'] == 'SUCCESS') {
  504. // //根据返回的订单号做业务逻辑
  505. // Log::info(json_encode($data));
  506. // }else{//支付失败,输出错误信息
  507. // fwrite($file,date("Y-m-d H:i:s")."错误信息:支付失败,".json_encode($data).PHP_EOL);
  508. // }
  509. // }else{
  510. // fwrite($file,date("Y-m-d H:i:s")."错误信息:签名验证失败,".json_encode($data).PHP_EOL);
  511. // }
  512. }
  513. public static function FromXml($xml)
  514. {
  515. if (!$xml) {
  516. echo "xml数据异常!";
  517. }
  518. //将XML转为array
  519. //禁止引用外部xml实体
  520. libxml_disable_entity_loader(true);
  521. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  522. return $data;
  523. }
  524. //生成签名
  525. public static function getSign($params, $api_key)
  526. {
  527. ksort($params); //将参数数组按照参数名ASCII码从小到大排序
  528. foreach ($params as $key => $item) {
  529. if (!empty($item)) { //剔除参数值为空的参数
  530. $newArr[] = $key . '=' . $item; // 整合新的参数数组
  531. }
  532. }
  533. $stringA = implode("&", $newArr); //使用 & 符号连接参数
  534. $stringSignTemp = $stringA . "&key=" . $api_key; //拼接key
  535. // key是在商户平台API安全里自己设置的
  536. $stringSignTemp = MD5($stringSignTemp); //将字符串进行MD5加密
  537. $sign = strtoupper($stringSignTemp); //将所有字符转换为大写
  538. return $sign;
  539. }
  540. //提现状态查询
  541. public function tuikuanchaxun()
  542. {
  543. $refund_id ='50301107102021012605823556180';// '50300706442020120904591342059';
  544. $sub_mchid ='1604839213';// '1604640016';
  545. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/refunds/id/' . $refund_id . '?sub_mchid=' . $sub_mchid;//查询地址
  546. $merchant_id = "1604585534";//商户号
  547. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  548. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  549. $timestamp = time();//时间戳
  550. $nonce = $this->nonce_str();//随机字符串
  551. $body = "";
  552. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  553. $header = [
  554. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  555. 'Accept:application/json',
  556. 'User-Agent:' . $merchant_id,
  557. 'Content-Type:application/json',
  558. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  559. ];
  560. $result = $this->curl($url, '', $header, 'GET');
  561. $result = json_decode($result, true);
  562. return $this->success($result);
  563. }
  564. //企业付款到用户零钱
  565. public function companyPayCash(){
  566. // $sub_mchid = '1604640016';
  567. $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';//查询地址
  568. $merchant_id = "1604585534";//商户号
  569. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  570. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  571. $timestamp = time();//时间戳
  572. $nonce = $this->nonce_str();//随机字符串
  573. $data = [
  574. 'mchid' => $merchant_id,
  575. 'mch_appid' => config('wechat.payment.default.app_id'),
  576. 'partner_trade_no' => 'CP' . date('ymdHis') . substr(microtime(), 2, 6) . mt_rand(10000, 99999),
  577. 'openid' => 'oJn4Fv3GoK8rMqftML5kSU8FoDUc',
  578. 'check_name' => 'NO_CHECK',
  579. 'amount' => 1,
  580. 'desc' => '退款',
  581. 'nonce_str' =>$nonce,
  582. ];
  583. $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
  584. return $sign['signature'];
  585. $data['sign']=$sign->signature;
  586. $data=$this->arraytoxml($data);
  587. return $data;
  588. $header = [
  589. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  590. 'Accept:application/json',
  591. 'User-Agent:' . $merchant_id,
  592. 'Content-Type:application/json',
  593. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  594. ];
  595. $result = $this->curl($url, json_encode($data), $header, 'POST');
  596. return $result;
  597. Log::info($result);
  598. $result = json_decode($result, true);
  599. return $this->success($result);
  600. }
  601. function arraytoxml($data){
  602. $str='<xml>';
  603. foreach($data as $k=>$v) {
  604. $str.='<'.$k.'>'.$v.'</'.$k.'>';
  605. }
  606. $str.='</xml>';
  607. return $str;
  608. }
  609. //获取资金账单下载url
  610. public function zijinzhangdan()
  611. {
  612. $bill_date = '2020-12-09';
  613. $account_type = 'BASIC';
  614. $tar_type = 'GZIP';
  615. $sub_mchid = '1604640016';
  616. $url = 'https://api.mch.weixin.qq.com/v3/bill/fundflowbill?bill_date=' . $bill_date . '&account_type=' . $account_type . '&tar_type=' . $tar_type;//查询地址
  617. $merchant_id = "1604585534";//商户号
  618. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  619. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  620. $timestamp = time();//时间戳
  621. $nonce = $this->nonce_str();//随机字符串
  622. $body = "";
  623. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  624. $header = [
  625. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  626. 'Accept:application/json',
  627. 'User-Agent:' . $merchant_id,
  628. 'Content-Type:application/json',
  629. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  630. ];
  631. $result = $this->curl($url, '', $header, 'GET');
  632. $result = json_decode($result, true);
  633. return $this->success($result);
  634. }
  635. //获取交易账单下载url
  636. public function jiaoyizhangdan()
  637. {
  638. $bill_date = '2020-12-09';
  639. $account_type = 'ALL';
  640. $tar_type = 'GZIP';
  641. // $sub_mchid='1604640016';
  642. $sub_mchid = '';//'&sub_mchid='.$sub_mchid.
  643. $url = 'https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=' . $bill_date . '&account_type=' . $account_type . '&tar_type=' . $tar_type;//查询地址
  644. $merchant_id = "1604585534";//商户号
  645. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  646. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  647. $timestamp = time();//时间戳
  648. $nonce = $this->nonce_str();//随机字符串
  649. $body = "";
  650. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  651. $header = [
  652. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  653. 'Accept:application/json',
  654. 'User-Agent:' . $merchant_id,
  655. 'Content-Type:application/json',
  656. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  657. ];
  658. $result = $this->curl($url, '', $header, 'GET');
  659. $result = json_decode($result, true);
  660. return $this->success($result);
  661. }
  662. //下载账单
  663. public function downzhangdan()
  664. {
  665. $url = 'https://api.mch.weixin.qq.com/v3/billdownload/file?token=rpsaygCj-6NgWW-evyqxZ-yj-AE6cfd9KKctbebu80VBuBQL-I1x0ioq6lQ4pwSC&tartype=gzip';//查询地址
  666. $merchant_id = "1604585534";//商户号
  667. $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
  668. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  669. $timestamp = time();//时间戳
  670. $nonce = $this->nonce_str();//随机字符串
  671. $body = "";
  672. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  673. $header = [
  674. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  675. 'Accept:application/json',
  676. 'User-Agent:' . $merchant_id,
  677. 'Content-Type:application/json',
  678. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  679. ];
  680. $result = $this->curl($url, '', $header, 'GET');
  681. return $result;
  682. $result = json_decode($result, true);
  683. return $this->success($result);
  684. }
  685. //导入
  686. public function getImport(Request $request)
  687. {
  688. ini_set('memory_limit', '1024M');
  689. Cache::put('page_city', '123',-1);
  690. $page = Cache::get('page_city') ?? 0;
  691. $size = 300;
  692. $num = $page * $size;
  693. $bank123 = Bank123::orderBy('sub_branch_id')->skip($num)->take($size)->get();
  694. $region=Region::where('level',2)->get();
  695. foreach ($bank123 as $key => $val) {
  696. foreach($region as $k=>$v){
  697. if(strpos($val->sub_branch_name,$v->sname)){
  698. Log::info( $page.'//'.$val->sub_branch_name . '//' . $v->sname);
  699. $name=explode(',',$v->mername);
  700. Bank123::where('sub_branch_id',$val->sub_branch_id)->update(['city'=>$v->name,'province'=>$name[1]]);
  701. }
  702. }
  703. }
  704. $page++;
  705. Cache::put('page_city', $page);
  706. return '123';
  707. // Cache::put('page', '123',-1);
  708. // return $page = Cache::get('page');
  709. $page = Cache::get('page') ?? 0;
  710. $size = 300;
  711. $num = $page * $size;
  712. // $banks = Bank::where('bank_name', '工商银行')->get();
  713. $bank123 = Bank123::orderBy('sub_branch_id')->skip($num)->take($size)->get();
  714. foreach ($bank123 as $key => $val) {
  715. Bank123::where('sub_branch_id',$val->sub_branch_id)->update(['bank'=>mb_substr($val->sub_branch_name,0,6)]);
  716. }
  717. $page++;
  718. Cache::put('page', $page);
  719. return $page . '//' . $size;
  720. return 1;
  721. $name = '上海市';
  722. $city_id = '3101';
  723. $province_id = '31';
  724. // $row=Bank::where('province',$name)->update(['city_id'=>$city_id,'province_id'=>$province_id,'city'=>$name]);
  725. $row = Bank::where('city', $name)->update(['city_id' => $city_id, 'province_id' => $province_id]);
  726. return $row;
  727. $json = file_get_contents('C:\Users\001\Desktop\123123.json');
  728. // return $json[0];
  729. return json_decode($json, true);
  730. return 1;
  731. $page = Cache::get('page') ?? 17;
  732. $page++;
  733. Cache::put('page', $page);
  734. $size = 200;
  735. $num = $page * $size;
  736. $banks = Bank::where('bank_name', '工商银行')->get();
  737. $bank123 = Bank123::skip($num)->take($size)->get();
  738. foreach ($banks as $key => $val) {
  739. foreach ($bank123 as $k => $v) {
  740. if ($val->sub_branch_name == $v->sub_branch_name) {
  741. Bank::where('bank_name', '工商银行')->where('sub_branch_name', $v->sub_branch_name)->update(['sub_branch_id' => $v->sub_branch_id]);
  742. }
  743. }
  744. }
  745. return $page . '//' . $size;
  746. $file = $request->file('file');
  747. if (!$file->isValid()) { //判断文件是否有效
  748. return $this->error('文件上传失败,请重新上传');
  749. }
  750. $array = Excel::toArray(new ExpressImport, request()->file('file'));
  751. $arr = $array[0];// array_slice($array[0],1);
  752. // return $arr;
  753. $data = [];
  754. $aa = [];
  755. foreach ($arr as $key => $val) {
  756. $data[$key]['sub_branch_name'] = $val[1];
  757. $data[$key]['sub_branch_id'] = $val[0];
  758. $aa[] = $val[0];
  759. }
  760. // return $aa;
  761. Bank::whereIn('sub_branch_id', $aa)->delete();
  762. Bank::insert($data);
  763. // Log::info($a);
  764. }
  765. //$apicode = "30422c9cbb1f4fed9f1c64b31197d7d1";
  766. //
  767. //$params = array(
  768. //"bankType" => "102",
  769. //"queryName" => "北京",
  770. //// "cbdocvo" => "",
  771. //
  772. //);
  773. public function getwww()
  774. {
  775. header('Content-type:text/html;charset=utf-8');
  776. //配置您申请的appkey
  777. $apicode = "30422c9cbb1f4fed9f1c64b31197d7d1";
  778. $url = "https://api.yonyoucloud.com/apis/dst/BankDotQuery/bankTypeQuery";
  779. $method = "GET";
  780. // $params = array();
  781. $params = array(
  782. "bankType" => "102",
  783. "queryName" => "北京",
  784. );
  785. $header = array();
  786. $header[] = "apicode:" . $apicode;
  787. $header[] = "content-type:application/json";
  788. $content = $this->linkcurl($url, $method, $params, $header);
  789. return $content;
  790. // return $this->success($content);
  791. $result = json_decode($content, true);
  792. // return json_decode($result['result'],true);
  793. // return json_decode($result,true);
  794. if ($result) {
  795. if ($result['error_code'] == '0') {
  796. print_r($result);
  797. } else {
  798. echo $result['error_code'] . ":" . $result['reason'];
  799. }
  800. } else {
  801. echo "请求失败";
  802. }
  803. }
  804. /**
  805. * 请求接口返回内容
  806. * @param string $url [请求的URL地址]
  807. * @param string $params [请求的参数]
  808. * @param int $ipost [是否采用POST形式]
  809. * @return string
  810. */
  811. function linkcurl($url, $method, $params = false, $header = false)
  812. {
  813. $httpInfo = array();
  814. $ch = curl_init();
  815. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  816. curl_setopt($ch, CURLOPT_URL, $url);
  817. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  818. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  819. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  820. if (1 == strpos("$" . $url, "https://")) {
  821. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  822. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  823. }
  824. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
  825. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  826. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  827. if ($method == "POST") {
  828. curl_setopt($ch, CURLOPT_POST, true);
  829. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  830. } else if ($params) {
  831. curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
  832. }
  833. $response = curl_exec($ch);
  834. if ($response === FALSE) {
  835. //echo "cURL Error: " . curl_error($ch);
  836. return false;
  837. }
  838. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  839. $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
  840. curl_close($ch);
  841. return $response;
  842. }
  843. public function getBank()
  844. {
  845. header('Content-type:text/html;charset=utf-8');
  846. //配置您申请的appkey
  847. $apicode = "30422c9cbb1f4fed9f1c64b31197d7d1";
  848. // $url = "https://api.yonyoucloud.com/apis/u8c/uapbd/custdoc_insert";
  849. $url = "https://api.yonyoucloud.com/apis/dst/BankDotQuery/bankDotQuery";
  850. $method = "GET";
  851. $params = array(
  852. "bankType" => "102",
  853. "queryName" => "北京",
  854. );
  855. // $params = array(
  856. // "cbdocvo" => "",
  857. //
  858. // );
  859. $header = array();
  860. $header[] = "apicode:" . $apicode;
  861. $header[] = "content-type:application/json";
  862. $header[] = "trantype:code";
  863. $content = $this->wlinkcurl($url, $method, $params, $header);
  864. $result = json_decode($content, true);
  865. return $result;
  866. if ($result) {
  867. if ($result['error_code'] == '0') {
  868. print_r($result);
  869. } else {
  870. echo $result['error_code'] . ":" . $result['reason'];
  871. }
  872. } else {
  873. echo "请求失败";
  874. }
  875. }
  876. /**
  877. * 请求接口返回内容
  878. * @param string $url [请求的URL地址]
  879. * @param string $params [请求的参数]
  880. * @param int $ipost [是否采用POST形式]
  881. * @return string
  882. */
  883. public function wlinkcurl($url, $method, $params = false, $header = false)
  884. {
  885. $httpInfo = array();
  886. $ch = curl_init();
  887. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  888. curl_setopt($ch, CURLOPT_URL, $url);
  889. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  890. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  891. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  892. if (1 == strpos("$" . $url, "https://")) {
  893. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  894. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  895. }
  896. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
  897. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  898. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  899. if ($method == "POST") {
  900. curl_setopt($ch, CURLOPT_POST, true);
  901. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  902. } else if ($params) {
  903. curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
  904. }
  905. $response = curl_exec($ch);
  906. if ($response === FALSE) {
  907. //echo "cURL Error: " . curl_error($ch);
  908. return false;
  909. }
  910. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  911. $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
  912. curl_close($ch);
  913. return $response;
  914. }
  915. }