首页 > 微信 > 详细

个人封装的php对微信的一些方法 原生

时间:2021-01-07 10:51:52      阅读:16      评论:0      收藏:0      [点我收藏+]
/*判断是否微信环境*/
function is_weixin()
{
    if (strpos($_SERVER[‘HTTP_USER_AGENT‘], ‘MicroMessenger‘) !== false) {
        return true;
    }
    return false;
}

/**
 * 获取wx_code的URL
 * 1获取code
 * 2获取access_token
 * 3获取userinfo
 * 4获取通用token
 * 5获取ticket
* 6发送模板
* 7 获取用户信息2(是否关注) */ function get_wx_url($param) { if ($param[‘type‘] == 1) { $arr = [ ‘appid‘ => $param[‘appid‘], ‘redirect_uri‘ => $param[‘url‘], ‘response_type‘ => ‘code‘, ‘scope‘ => ‘snsapi_userinfo‘, ‘state‘ => 1 ]; $param = http_build_query($arr); return "https://open.weixin.qq.com/connect/oauth2/authorize?" . $param . "#wechat_redirect"; } elseif ($param[‘type‘] == 2) { return "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $param[‘appid‘] . "&secret=" . $param[‘secret‘] . "&code=" . $param[‘code‘] . "&grant_type=authorization_code"; } elseif ($param[‘type‘] == 3) { return "https://api.weixin.qq.com/sns/userinfo?access_token=" . $param[‘access_token‘] . "&openid=" . $param[‘openid‘] . "&lang=zh_CN"; } elseif ($param[‘type‘] == 4) { return "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $param[‘appid‘] . "&secret=" . $param[‘secret‘]; } elseif ($param[‘type‘] == 5) { return "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $param[‘access_token‘] . "&type=jsapi"; } elseif ($param[‘type‘] == 6) { return "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $param[‘access_token‘]; } elseif ($param[‘type‘] == 7) { return "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $param[‘access_token‘] . "&openid=" . $param[‘openid‘] . "&lang=zh_CN"; } } /**获取微信token 有效期2 hours*/ function get_token($param) { if (!Cache::get(‘access_token‘)) { $url = get_wx_url([‘type‘ => 4, ‘appid‘ => $param[‘appid‘], ‘secret‘ => $param[‘secret‘]]); $re1 = Http::sendRequest($url); $access_token = json_decode($re1[‘msg‘])->access_token; Cache::set(‘access_token‘, $access_token, 3000); } else { $access_token = Cache::get(‘access_token‘); } return $access_token; } /*获取openid*/ function get_openid($param) { $url = get_wx_url( [ ‘type‘ => 1, ‘appid‘ => $param[‘site‘][‘wx_appid‘], ‘url‘ => $param[‘site‘][‘domain‘] . "api/index/openid" ] ); if ($param && isset($param[‘code‘])) { $access_token_url = get_wx_url( [ ‘type‘ => 2, ‘appid‘ => $param[‘site‘][‘wx_appid‘], ‘secret‘ => $param[‘site‘][‘wx_secret‘], ‘code‘ => $param[‘code‘] ] ); $re = Http::sendRequest($access_token_url, [], ‘GET‘); if ($re[‘ret‘] == 1 && $re[‘msg‘]) { $msg = json_decode($re[‘msg‘]); if (isset($msg->errcode)) { echo ‘<pre>‘; print_r($msg); exit; } else { $access_token = $msg->access_token; $openid = $msg->openid; return $openid; } } } else { header("Location:" . $url); exit; } } /*微信登录*/ function wx_login($param) { $url = get_wx_url( [ ‘type‘ => 1, ‘appid‘ => $param[‘site‘][‘wx_appid‘], ‘url‘ => $param[‘site‘][‘domain‘] ] ); if ($param && isset($param[‘code‘])) { $access_token_url = get_wx_url( [ ‘type‘ => 2, ‘appid‘ => $param[‘site‘][‘wx_appid‘], ‘secret‘ => $param[‘site‘][‘wx_secret‘], ‘code‘ => $param[‘code‘] ] ); $re = Http::sendRequest($access_token_url, [], ‘GET‘); if ($re[‘ret‘] == 1 && $re[‘msg‘]) { $msg = json_decode($re[‘msg‘]); if (isset($msg->errcode)) { echo ‘<pre>‘; print_r($msg); exit; } else { $access_token = $msg->access_token; $openid = $msg->openid; } } $user_info_url = get_wx_url( [ ‘type‘ => 3, ‘access_token‘ => $access_token, ‘openid‘ => $openid ] ); $userinfo = Http::sendRequest($user_info_url, [], ‘GET‘); if ($re[‘ret‘] == 1 && $re[‘msg‘]) { $msg = json_decode($re[‘msg‘]); if (isset($msg->errcode)) { echo ‘<pre>‘; print_r($msg); exit; } else { /*判断用户的存在情况 微信注册新用户*/ $info = json_decode($userinfo[‘msg‘]); /*echo ‘<pre>‘; print_r($info); exit;*/ /*2020年1月18日09:51:14 判断用户关注情况 1获取通用token 2获取关注信息*/ $param = [ ‘appid‘ => $param[‘site‘][‘wx_appid‘], ‘secret‘ => $param[‘site‘][‘wx_secret‘], ]; $token = get_token($param); $sub_user_info_url = get_wx_url( [ ‘type‘ => 7, ‘access_token‘ => $token, ‘openid‘ => $openid ] ); $subuserinfo = Http::sendRequest($sub_user_info_url, [], ‘GET‘); $subuserinfo = json_decode($subuserinfo[‘msg‘]); $data = [‘avatar‘ => $info->headimgurl, ‘openid‘ => $info->openid, ‘nickname‘ => $info->nickname, ‘is_subscrib‘ => $subuserinfo->subscribe, ]; $user = User::where([‘openid‘ => $info->openid])->find(); if ($user) { if ($user[‘nickname‘]) { unset($data[‘nickname‘]); } if (!$user[‘avatar‘] || !file_exists($user[‘avatar‘])) { /*将头像储存在本地*/ $file = getImage($data[‘avatar‘]); $path = "../public/uploads/" . date(‘Ymd‘) . "/avatar/"; if (!is_dir($path)) { mkdir($path, 0777, true); } $avatar_file_name = $path . date(‘YmdHis‘) . $data[‘openid‘] . ".jpg"; // $re = $file->save($qrcode_file_name); file_put_contents($avatar_file_name, $file); $data[‘avatar‘] = str_replace("../public", "", $avatar_file_name); } else { unset($data[‘avatar‘]); } User::where([‘openid‘ => $info->openid])->update($data); } else { /*将头像储存在本地*/ $file = getImage($data[‘avatar‘]); $path = "../public/uploads/" . date(‘Ymd‘) . "/avatar/"; if (!is_dir($path)) { mkdir($path, 0777, true); } $avatar_file_name = $path . date(‘YmdHis‘) . $data[‘openid‘] . ".jpg"; // $re = $file->save($qrcode_file_name); file_put_contents($avatar_file_name, $file); $data[‘avatar‘] = str_replace("../public", "", $avatar_file_name); $data[‘createtime‘] = $data[‘jointime‘] = time(); $user = User::create($data); $team = new Team(); $team::create([‘user_id‘ => $user->id]); } $re_url = session(‘re_url‘); session(‘uid‘, $user[‘id‘]); header("Location:" . $re_url); } } } else { if (!session(‘re_url‘)) { session(‘re_url‘, $param[‘re_url‘]); } if (!session(‘uid‘)) { header("Location:" . $url); exit; } } } function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } /*获取微信jssdk ticket 1获取access_token*/ function get_ticket($param) { //return ‘‘; if (Cache::get(‘jsticket‘) == ‘123123‘) { Cache::set(‘jsticket‘, 0); } if (Cache::get(‘access_token‘) == ‘123123‘) { Cache::set(‘access_token‘, 0); } if (!Cache::get(‘jsticket‘)) { if (!Cache::get(‘access_token‘)) { $url = get_wx_url([‘type‘ => 4, ‘appid‘ => $param[‘site‘][‘wx_appid‘], ‘secret‘ => $param[‘site‘][‘wx_secret‘]]); $re1 = Http::sendRequest($url); $access_token = json_decode($re1[‘msg‘])->access_token; Cache::set(‘access_token‘, $access_token, 4500); } else { $access_token = Cache::get(‘access_token‘); } if ($access_token) { $ticket_url = get_wx_url([‘type‘ => 5, ‘access_token‘ => $access_token]); $re = Http::sendRequest($ticket_url); if ($re) { $jsticket = json_decode($re[‘msg‘])->ticket; Cache::set(‘jsticket‘, $jsticket, 4500); } } } else { $jsticket = Cache::get(‘jsticket‘); $access_token = Cache::get(‘access_token‘); } $timestamp = time(); $noncestr = createNonceStr(); // 注意 URL 建议动态获取(也可以写死). $protocol = (!empty($_SERVER[‘HTTPS‘]) && $_SERVER[‘HTTPS‘] !== ‘off‘ || $_SERVER[‘SERVER_PORT‘] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //$url = "http://kangaroo.vshop365.cn/"; // 调用JSSDK的页面地址 $str = "jsapi_ticket={$jsticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$url}"; // 进行sha1签名 $sha_str = sha1($str); return [‘ticket‘ => $jsticket, ‘url‘ => $url, ‘access_token‘ => $access_token, ‘timestamp‘ => $timestamp, ‘noncestr‘ => $noncestr, ‘signature‘ => $sha_str, ‘appid‘ => $param[‘site‘][‘wx_appid‘]]; } /*发送模板消息*/ function send_template($param) { if (!Cache::get(‘access_token‘)) { $url = get_wx_url([‘type‘ => 4, ‘appid‘ => $param[‘site‘][‘wx_appid‘], ‘secret‘ => $param[‘site‘][‘wx_secret‘]]); $re1 = Http::sendRequest($url); $access_token = json_decode($re1[‘msg‘])->access_token; Cache::set(‘access_token‘, $access_token, 4500); } else { $access_token = Cache::get(‘access_token‘); } if ($access_token) { $ticket_url = get_wx_url([‘type‘ => 6, ‘access_token‘ => $access_token]); $re = Http::sendRequest($ticket_url, $param[‘data‘]); if ($re) { return $re; } } }

  

个人封装的php对微信的一些方法 原生

原文:https://www.cnblogs.com/Ychao/p/14244928.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!