CommonServer.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <?php
  2. namespace App\Servers;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Ecs\Ecs;
  5. use AlibabaCloud\Green\Green;
  6. use App\Models\Config;
  7. use App\Models\ErrorRecord;
  8. use App\Models\Region;
  9. use Illuminate\Support\Facades\Storage;
  10. use AlibabaCloud\Client\Exception\ClientException;
  11. use AlibabaCloud\Client\Exception\ServerException;
  12. use QCloud\COSSTS\Sts;
  13. /**
  14. * Redis数据缓存类
  15. */
  16. class CommonServer
  17. {
  18. /**
  19. * 错误信息
  20. * @var string
  21. */
  22. private $errorMsg = '';
  23. static private $server = null;
  24. private function __construct()
  25. {
  26. }
  27. /**
  28. * 创建对象
  29. * @return CommonServer
  30. */
  31. static function creatServer()
  32. {
  33. if (empty(self::$server)) self::$server = new CommonServer();
  34. return self::$server;
  35. }
  36. /**
  37. * 验证手机号码格式
  38. * @param $phone
  39. * @return bool
  40. */
  41. public function verifyPhoneNumber($phone)
  42. {
  43. if (empty($phone)) return false;
  44. if (!preg_match('#^[\d]{11,11}$#', $phone)) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * 字符串去标签过滤及空格过滤
  51. * @param $str_name
  52. * @param $default
  53. * @return string
  54. */
  55. function filtrationStr($str_name, $default = '')
  56. {
  57. $str = request()->input($str_name, $default);
  58. if (empty($str)) return $str;
  59. if (!is_string($str)) {
  60. $str = json_encode($str);
  61. $str = strip_tags($str);
  62. $str = trim($str);
  63. $str = json_decode($str, true);
  64. } else {
  65. $str = strip_tags($str);
  66. $str = trim($str);
  67. }
  68. return $str;
  69. }
  70. /**
  71. * 获取城市名称
  72. * @param string $province_id
  73. * @param string $city_id
  74. * @param string $area_id
  75. * @return string
  76. */
  77. function getCityName($province_id = '', $city_id = '', $area_id = '')
  78. {
  79. $ids = [$province_id, $city_id, $area_id];
  80. $ids = array_filter($ids);
  81. if (empty($ids)) return '';
  82. $city_name = Region::whereIn('id', $ids)->orderBy('id', 'asc')->pluck('name')->toArray();
  83. $city_name = implode(' ', $city_name);
  84. return $city_name;
  85. }
  86. /**
  87. * 字符串屏蔽
  88. * @param $str
  89. * @param int $start_key 开始位置
  90. * @param int $end_key 尾数开始位置
  91. * @param int $start_num 截取位数
  92. * @param int $end_num 倒数截取位数
  93. * @param int $conceal_num 最大填充数量
  94. * @return string
  95. */
  96. function concealStr($str, $start_key = 0, $end_key = 1, $start_num = 1, $end_num = 1, $conceal_num = 3)
  97. {
  98. $str_len = mb_strlen($str);
  99. $start_str = '';
  100. $end_str = '';
  101. if (($str_len - $start_num - $end_num) < $conceal_num) {
  102. $conceal_num = $str_len - $start_num - $end_num;
  103. }
  104. $conceal_str = $this->getRandChar($conceal_num, '*');
  105. if (empty($conceal_str)) $conceal_str = '*';
  106. if ($str_len >= $start_key) {
  107. $start_str = mb_substr($str, $start_key, $start_num);
  108. }
  109. if ($str_len > $end_key) {
  110. $end_str = mb_substr($str, $end_key * -1, $end_num);
  111. }
  112. return $start_str . $conceal_str . $end_str;
  113. }
  114. /**
  115. * 获取随机字符串
  116. * @param int $length
  117. * @param string $strPol
  118. * @return string|null
  119. */
  120. public function getRandChar($length = 6, $strPol = '')
  121. {
  122. if ($length <= 0) $length = 1;
  123. $str = null;
  124. if (empty($strPol)) $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  125. $max = strlen($strPol) - 1;
  126. for ($i = 0; $i < $length; $i++) {
  127. $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
  128. }
  129. return $str;
  130. }
  131. /**
  132. * 获取数据库配置信息
  133. * @param $key
  134. * @return mixed
  135. */
  136. function getConfigValue($key)
  137. {
  138. return Config::where('key', $key)->value('value');
  139. }
  140. /**
  141. * 创建文件夹
  142. * @param $dir
  143. * @return bool
  144. */
  145. function creatDir($dir)
  146. {
  147. if ($dir == '') return false;
  148. if (!file_exists(public_path($dir))) {
  149. mkdir(public_path($dir), 0777, true);
  150. chmod(public_path($dir), 0777);
  151. }
  152. return true;
  153. }
  154. /**
  155. * 获取本月1号至月底
  156. * @param $date
  157. * @return array
  158. */
  159. function getNextMonth($date)
  160. {
  161. $first_day = date("Y-m-01 00:00:00", strtotime($date));
  162. $next_day = date("Y-m-d 23:59:59", strtotime("$first_day +1 month -1 day"));
  163. return [$first_day, $next_day];
  164. }
  165. /**
  166. * 获取多少天后的日期
  167. * @param $date
  168. * @param $num
  169. * @return false|string
  170. */
  171. function getManyDate($date, $num)
  172. {
  173. if(!is_numeric($num) || $num==0){
  174. return $date;
  175. }
  176. if($num>0){
  177. $next_day = date("Y-m-d", strtotime("$date +$num day"));
  178. }else{
  179. $next_day = date("Y-m-d", strtotime("$date $num day"));
  180. }
  181. return $next_day;
  182. }
  183. /**
  184. * 验证是否微信浏览器
  185. * @return bool
  186. */
  187. function isWeixin()
  188. {
  189. $http_user_agent = request()->header('user-agent');
  190. if (strpos($http_user_agent, 'MicroMessenger') !== false) {
  191. return true;
  192. }
  193. return false;
  194. }
  195. /**
  196. * 格式化小数位数
  197. * @param $num
  198. * @param int $digit
  199. * @return string
  200. */
  201. function setFloatNum($num, $digit = 4)
  202. {
  203. return sprintf("%.{$digit}f", $num);
  204. }
  205. /**
  206. * 获取真实IP
  207. * @return mixed
  208. */
  209. function getClientIp()
  210. {
  211. $http_x_forwarded_for = request()->header('x-forwarded-for');
  212. if (empty($http_x_forwarded_for)) {
  213. return request()->getClientIp();
  214. } else {
  215. $http_x_forwarded_for = explode(',', $http_x_forwarded_for);
  216. return $http_x_forwarded_for[0];
  217. }
  218. }
  219. function delDir($dir)
  220. {
  221. //先删除目录下的文件:
  222. $dh = opendir($dir);
  223. while ($file = readdir($dh)) {
  224. if ($file != "." && $file != "..") {
  225. $fullpath = $dir . "/" . $file;
  226. if (!is_dir($fullpath)) {
  227. unlink($fullpath);
  228. } else {
  229. $this->deldir($fullpath);
  230. }
  231. }
  232. }
  233. closedir($dh);
  234. //删除当前文件夹:
  235. if (rmdir($dir)) {
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. }
  241. /**
  242. * 图片base64
  243. * @param $image_file
  244. * @return string
  245. */
  246. function base64EncodeImage($image_file)
  247. {
  248. $image_info = getimagesize($image_file);
  249. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  250. $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  251. return $base64_image;
  252. }
  253. /**
  254. * 图片上传OSS
  255. * @param $img
  256. * @param string $path
  257. * @param $is_compress
  258. * @return string
  259. */
  260. function uploadingImg($img, $path = 'headimgurl',$is_compress=true)
  261. {
  262. $base64_image_content = file_get_contents($img);
  263. $upload_path = "storage" . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . date('Ymd', time()) . DIRECTORY_SEPARATOR;
  264. $img_url = md5(uniqid() . time()) . '.png';
  265. $upload_path1 = public_path($upload_path);
  266. if (!is_dir($upload_path1)) {
  267. mkdir($upload_path1, 0777, true);
  268. }
  269. $oss = Storage::disk('oss');
  270. $pathA = "{$upload_path}{$img_url}";
  271. $update = $oss->put($pathA, $base64_image_content);
  272. if (!$update) { // 如果失败上传本地
  273. file_put_contents($upload_path1 . $img_url, base64_decode($base64_image_content));
  274. }
  275. return $update ? ($oss->url($pathA) . ($is_compress?'?x-oss-process=image/auto-orient,1/quality,q_85':'')) : asset($pathA);
  276. }
  277. /**
  278. * OSS图片压缩
  279. * @param $img
  280. * @param int $resize (3,10,30,50)
  281. * @param int $type 1原比例压缩,2固定高宽比列压缩(300*300)
  282. * @return string|string[]
  283. */
  284. function ossCompress($img, $resize = 10, $type = 1)
  285. {
  286. $old_str=strrchr($img, '?');
  287. if($old_str)$img=str_replace($old_str, '', $img);
  288. if ($type == 1) {
  289. return $img . '?x-oss-process=image/auto-orient,1/resize,p_' . $resize . '/quality,q_90';
  290. } elseif ($type == 2) {
  291. return $img . '?x-oss-process=image/auto-orient,1/resize,m_fill,w_300,h_300/quality,q_90';
  292. } elseif ($type == 3) {
  293. return $img . '?x-oss-process=image/auto-orient,1/resize,m_lfit,w_780/quality,q_100';
  294. }
  295. return $img;
  296. }
  297. /**
  298. * 保留2为小数
  299. * @param $num
  300. * @param int $format_num
  301. * @return float|int
  302. */
  303. function setFormatNum($num,$format_num=2){
  304. // return $num;
  305. $format_num=4;
  306. if(!is_numeric($num)){
  307. return 0;
  308. }
  309. if($format_num<0){
  310. return $num;
  311. }elseif ($format_num==0){
  312. return floor($num);
  313. }else{
  314. $num= sprintf("%.{$format_num}f", $num);
  315. // $multiple=pow(10,$format_num);
  316. // $num = floor($num*$multiple)/$multiple;
  317. }
  318. $num=$num*1;
  319. return $num;
  320. }
  321. /**
  322. * oss图片删除
  323. * @param $path
  324. * @return bool
  325. */
  326. function delImg($path)
  327. {
  328. $path_num = stripos($path, '?');
  329. if ($path_num !== false) {
  330. $path = substr($path, 0, $path_num);
  331. }
  332. $path = str_replace('https://jhnewshop.oss-cn-chengdu.aliyuncs.com', '', $path);
  333. $oss = Storage::disk('oss');
  334. $update = $oss->delete($path);
  335. return $update;
  336. }
  337. /**
  338. * 获取阿里云sts临时权限
  339. */
  340. function getAliSts()
  341. {
  342. $data = RedisDataServer::creatServer()->getData('sts_data');
  343. if ($data) {
  344. $data = json_decode($data, true);
  345. } else {
  346. $accessKeyId = env('ALI_OSS_ACCESS_ID');
  347. $accessSecret = env('ALI_OSS_ACCESS_KEY');
  348. AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)->regionId('cn-hangzhou')->asDefaultClient();
  349. //设置参数,发起请求。
  350. try {
  351. $result = AlibabaCloud::rpc()
  352. ->product('Sts')
  353. ->scheme('https') // https | http
  354. ->version('2015-04-01')
  355. ->action('AssumeRole')
  356. ->method('POST')
  357. ->host('sts.aliyuncs.com')
  358. ->options([
  359. 'query' => [
  360. 'RegionId' => "cn-chengdu",
  361. 'RoleArn' => "acs:ram::1470797691368660:role/oss-js",
  362. 'RoleSessionName' => "js-oss-serve",
  363. 'DurationSeconds' => 900,
  364. ],
  365. ])
  366. ->request();
  367. $result = $result->toArray();
  368. if (empty($result['Credentials'])) {
  369. return ['code' => 0, 'msg' => '获取token信息失败'];
  370. }
  371. $data = $result['Credentials'];
  372. unset($data['Expiration']);
  373. RedisDataServer::creatServer()->setData('sts_data', $data, 'json', 800);
  374. } catch (ClientException $e) {
  375. return ['code' => 0, 'msg' => $e->getErrorMessage()];
  376. } catch (ServerException $e) {
  377. return ['code' => 0, 'msg' => $e->getErrorMessage()];
  378. }
  379. }
  380. return ['code' => 1, 'msg' => '获取信息成功', 'data' => $data];
  381. }
  382. function getTxySts(){
  383. $data = RedisDataServer::creatServer()->getData('txy_sts_data');
  384. if ($data ) {
  385. $data = json_decode($data, true);
  386. } else {
  387. $sts = new Sts();
  388. $accessKeyId = env('TXY_OSS_ACCESS_ID');
  389. $accessSecret = env('TXY_OSS_ACCESS_KEY');
  390. $config = array(
  391. 'url' => 'https://sts.tencentcloudapi.com/', // url和domain保持一致
  392. 'domain' => 'sts.tencentcloudapi.com', // 域名,非必须,默认为 sts.tencentcloudapi.com
  393. 'proxy' => '',
  394. 'secretId' => $accessKeyId, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
  395. 'secretKey' => $accessSecret, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
  396. 'bucket' => 'wealfavor-1257406827', // 换成你的 bucket
  397. 'region' => 'ap-beijing', // 换成 bucket 所在园区
  398. 'durationSeconds' => 1800, // 密钥有效期
  399. 'allowPrefix' => 'app-serve/*', // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
  400. // 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
  401. 'allowActions' => array (
  402. // 简单上传
  403. 'name/cos:PutObject',
  404. 'name/cos:PostObject',
  405. // 分片上传
  406. 'name/cos:InitiateMultipartUpload',
  407. 'name/cos:ListMultipartUploads',
  408. 'name/cos:ListParts',
  409. 'name/cos:UploadPart',
  410. 'name/cos:CompleteMultipartUpload'
  411. )
  412. );
  413. // dd($config);
  414. try {
  415. $result = $sts->getTempKeys($config);
  416. if(empty($result['credentials'])){
  417. return ['code' => 0, 'msg' => '权限生成失败'];
  418. }
  419. $data = $result['credentials'];
  420. $data['StartTime']=time();
  421. $data['ExpiredTime']=time()+1700;
  422. RedisDataServer::creatServer()->setData('txy_sts_data', $data, 'json', 800);
  423. } catch (\Exception $e) {
  424. return ['code' => 0, 'msg' => $e->getMessage()];
  425. }
  426. }
  427. return ['code' => 1, 'msg' => '获取信息成功', 'data' => $data];
  428. }
  429. /**
  430. * 阿里云图片鉴定
  431. * @param $imges
  432. * @return array|bool
  433. * @throws ClientException
  434. * @throws ServerException
  435. */
  436. function sendAliImgAuth($imges)
  437. {
  438. if (is_string($imges)) $imges = [$imges];
  439. $tasks = [];
  440. $auth_status = 1;
  441. foreach ($imges as $key => $img) {
  442. $tasks[] = [
  443. 'dataId' => $key . '-img' . rand(10000000, 99999999) . '-' . time(),
  444. 'url' => $img,
  445. 'status' => 0,
  446. ];
  447. }
  448. $accessKeyId = env('ALI_OSS_ACCESS_ID');
  449. $accessSecret = env('ALI_OSS_ACCESS_KEY');
  450. AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)->regionId('cn-shanghai')->asDefaultClient();
  451. $request = Green::v20180509()->ImageSyncScan();
  452. $request->method('POST')
  453. ->scheme('https') // https | http
  454. ->body(json_encode([
  455. 'scenes' => ['porn', 'terrorism'],
  456. 'tasks' => $tasks,
  457. ]));
  458. $ret = $request->request();
  459. $ret_data = $ret->toArray();
  460. if (empty($ret_data['code']) || $ret_data['code'] != 200) {
  461. return $auth_status;
  462. } else {
  463. foreach ($ret_data['data'] as $key => $item) {
  464. $status = 1;
  465. if ($auth_status != 1) {
  466. break;
  467. }
  468. foreach ($item['results'] as $value) {
  469. if ($value['suggestion'] == 'review') {
  470. $status = 0;
  471. $auth_status = 0;
  472. break;
  473. } elseif ($value['suggestion'] == 'block') {
  474. $status = 2;
  475. $auth_status = 2;
  476. break;
  477. }
  478. }
  479. $tasks[$key]['status'] = $status;
  480. }
  481. }
  482. return ['auth_status' => $auth_status, 'tasks' => $tasks];
  483. }
  484. /**
  485. * 阿里云视频鉴定
  486. * @param $video 视频地址
  487. * @param int $type 鉴定类型
  488. * @param int $id 任务ID
  489. * @return bool
  490. * @throws ClientException
  491. * @throws ServerException
  492. */
  493. function sendAliVideoAuth($video, $type = 1, $id = 0)
  494. {
  495. $tasks = [
  496. [
  497. 'dataId' => $id . '-video' . rand(10000000, 99999999) . '-' . time(),
  498. 'url' => $video,
  499. 'interval' => 5,
  500. ]
  501. ];
  502. $accessKeyId = env('ALI_OSS_ACCESS_ID');
  503. $accessSecret = env('ALI_OSS_ACCESS_KEY');
  504. AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)->regionId('cn-shanghai')->asDefaultClient();
  505. $request = Green::v20180509()->videoAsyncScan();
  506. $request->method('POST')
  507. ->scheme('https') // https | http
  508. ->body(json_encode([
  509. 'callback' => route('notify.video-auth', ['type' => $type, 'id' => $id]),
  510. 'seed' => $id . '-video' . rand(10000000, 99999999),
  511. 'scenes' => ['porn', 'terrorism'],
  512. 'tasks' => $tasks,
  513. ]));
  514. $ret = $request->request();
  515. $ret_data = $ret->toArray();
  516. if (empty($ret_data['code']) || $ret_data['code'] != 200) {
  517. ErrorRecord::create(['msg' => '视频鉴定任务提交失败', 'data' => json_encode(compact('id', 'type', 'video'))]);
  518. return false;
  519. }
  520. return true;
  521. }
  522. /**
  523. * 获取文件名
  524. * @param $url
  525. * @return string
  526. */
  527. function getFileName($url)
  528. {
  529. $name = basename($url);
  530. return empty($name) ? '' : $name;
  531. }
  532. function lockCapitalRoutes($mId,$routeName,$type){
  533. $routeArr= [
  534. 'rush.session-pay',
  535. 'rush.order-pay',
  536. 'rush.putaway-goods',
  537. 'change.save',//划转
  538. 'dealOrder.create',//转赠
  539. 'member.set_wallet',//修改钱包地址
  540. 'order.create',//商城购买
  541. 'order.pay',//商城支付
  542. 'recharge.save',//充值提交
  543. 'withdraw.withdraw',//提现
  544. ];
  545. if(in_array($routeName,$routeArr)){
  546. $redisName=$mId.'lockCapital';
  547. if($type){
  548. $row= RedisDataServer::creatServer()->getData($redisName);
  549. $isOutput= RedisDataServer::creatServer()->getData('dateOutput');
  550. if($row || $isOutput){
  551. return false;
  552. }
  553. RedisDataServer::creatServer()->setData($redisName,time(),'str',3,false);
  554. }else{
  555. RedisDataServer::creatServer()->delData($redisName);
  556. }
  557. }
  558. return true;
  559. }
  560. //两个经纬度直接距离
  561. function getDistance($lat1, $lng1, $lat2, $lng2){
  562. $lat1 = ($lat1 * pi() ) / 180;
  563. $lng1 = ($lng1 * pi() ) / 180;
  564. $lat2 = ($lat2 * pi() ) / 180;
  565. $lng2 = ($lng2 * pi() ) / 180;
  566. $calcLongitude = $lng2 - $lng1;
  567. $calcLatitude = $lat2 - $lat1;
  568. $dist = 2 * asin(sqrt(pow(sin($calcLatitude/2),2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude/2),2)));
  569. $dist = $dist * 6378.137;
  570. return round($dist, 2);
  571. }
  572. function utf8_filter($str)
  573. {
  574. $pattern = '/[^\x{4e00}-\x{9fa5}\w]/u';
  575. // 使用正则表达式替换为空字符串
  576. $data = preg_replace($pattern, '', $str);
  577. return $data;
  578. }
  579. static function writeLog($text,$path='comm') {
  580. if(is_array($text) || is_object($text)){
  581. $text = json_encode($text,JSON_UNESCAPED_SLASHES);
  582. }
  583. $logDir = public_path('/storage/log/'.$path.'/'.date('Ymd').'/') ;
  584. if(!is_dir($logDir)){
  585. mkdir($logDir,0777,true);
  586. }
  587. file_put_contents ( $logDir.$path.date("Y-m-d").".log", date("Y-m-d H:i:s") . " Log" . $text . "\r\n", FILE_APPEND );
  588. }
  589. /**
  590. * @param $url
  591. * @param string $type 请求方式
  592. * @param string $data 数据 数组格式
  593. * @param $head
  594. * @param $retType
  595. * @return mixed
  596. */
  597. function sendRequest($url, $data = '', $type = 'get',$head=[],$retType='json')
  598. {
  599. $ch = curl_init();
  600. if ($type == 'get' && $data) {
  601. $url = $url . '?' . http_build_query($data);
  602. }
  603. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  604. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  605. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  606. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  607. if(empty($head)){
  608. $head = [
  609. 'User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
  610. 'Content-Type: application/json'
  611. ];
  612. }
  613. curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
  614. if ($type == 'post') {
  615. curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
  616. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  617. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置传递的参数
  618. }
  619. $request = curl_exec($ch);
  620. curl_close($ch);
  621. if($request){
  622. if($retType=='gzip'){
  623. $request=gzdecode($request);
  624. }
  625. $request=json_decode($request,true);
  626. }
  627. return $request;
  628. }
  629. }