123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\model;
- use think\cache\driver\Redis;
- use app\common\model\Wxapp;
- use think\facade\Cache;
- use app\common\exception\BaseException;
- use think\facade\Log;
-
- class Wechat {
- //返回配置 // 小程序id
- public static function getConfig($storeId = null){
- $data = Wxapp::getWxappCache();
- return ['app_id'=>$data['app_id'],'app_secret'=>$data['app_secret']];
- }
- //weixin token
- //"{"access_token":"27_vQnXeiUakHGc462ipuJZuNpK45gOdNKflz7GYWT1cVyy1CC6fhDemrvfFQWX3gRaieYU8tOPcZV6qkSHjtQpuK3OhYEq5935HZUcVSinQC8Osls61X9gArFP1sMuyUtblnjBUYX_DoeK4enDGDTaAJAXUK","expires_in":7200}"
- public function weixinTokenCache(){
- $key = 'ysc:weixin:token';
-
- $rds = new Redis(config('cache.stores.redis'));
- $tokenJson = $rds->get($key);
- $result = [];
- if($tokenJson){
- $result = json_decode($tokenJson,true);
- }
- else{
- $result = $this->weixinToken();
- $expires_in = $result['expires_in'];
- $expires = $expires_in - 7000;//设置一个比微信过期如7200更早的过期时间
- $rds->set($key,json_encode($result),$expires);
- }
- return $result;
- }
- //微信token
- public function weixinToken(){
- $config = $this->getConfig();
- $params["grant_type"] = "client_credential";
- $params["appid"] = $config['app_id'];
- $params["secret"] = $config['app_secret'];
- $url = "https://api.weixin.qq.com/cgi-bin/token?";
- $url = $url.http_build_query($params);
- $ret = $this->curl_request($url);
- $retArr = json_decode($ret,true);
- return $retArr;
- }
- private function getHttpArray($url,$post_data) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //没有这个会自动输出,不用print_r();也会在后面多个1
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- $output = curl_exec($ch);
- curl_close($ch);
- $out = json_decode($output);
- return $out;
- }
- //GET
- public function curl_request($url,$post='',$cookie='', $returnCookie=0){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- //设置https
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
- if($post) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
- }
- if($cookie) {
- curl_setopt($curl, CURLOPT_COOKIE, $cookie);
- }
- curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- if (curl_errno($curl)) {
- return curl_error($curl);
- }
- curl_close($curl);
- if($returnCookie){
- list($header, $body) = explode("\r\n\r\n", $data, 2);
- preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
- $info['cookie'] = substr($matches[1][0], 1);
- $info['content'] = $body;
- return $info;
- }else{
- return $data;
- }
- }
- /*
- POST
- */
- public function post_curl($posturl, $params, $method = 'POST', $header = '') {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $posturl);
- if ($header)
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- switch ($method) {
- case 'GET':
- curl_setopt($curl, CURLOPT_HTTPGET, 1);
- case 'POST':
- curl_setopt($curl, CURLOPT_POST, 1);
- break;
- case 'PUT':
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
- break;
- case 'DELETE':
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
- break;
- }
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, False);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($curl, CURLOPT_HEADER, FALSE);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $retdata = curl_exec($curl);
- curl_close($curl);
- return $retdata;
- }
- }
|