今天简单的介绍下PHP在微信中的应用,首先我们需要用到公众号中的appid 和 AppSecret 就可以开发了,看代码:
<?php
/**
* 使用原生
* 微信类
*/
class Wechat{
//定义常量
const APPID = '11122';
const APPSECRET = '3333';
//定义变量
public $access_token ; //微信access_token
public $redis;//redis
public $show;//返回
//实例化
public function __construct($redis_password = 123456,$redis_domain = '127.0.0.1',$redis_point = 6379,$redis_select = 0){
//返回参数
$show = $this->show = 1;
// function ($code = 1, $msg = '', $data = []){
// echo json_encode(array('code' => $code, 'msg' => $msg, 'data' => $data));
// exit;
// };
//实例化redis
$redis = new Redis();
$redis->connect($redis_domain, $redis_point);
$redis->auth($redis_password);
$redis->select($redis_select);
$this->redis = &$redis;
}
/**
* redis
*/
function redis():Redis{
return $this->redis;
}
/**
* 获取assaccess_token
* 地址:https://api.weixin.qq.com/sns/oauth2/access_token
*/
function get_access_token():string{
//access_token是否存在redis中
if(!$this->redis->exists('wechat_access_token')){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::APPID."&secret=".self::APPSECRET;
$res = json_decode(self::curl('GET', $url, []), true);
if (isset($res['access_token'])) {
$this->redis->set('wechat_access_token', $res['access_token'], ['ex'=> 7100]);
return $this->redis->get('wechat_access_token');
} else {
self::show(0, '获取ACCESS_TOKEN错误:'.$res['errcode'].",msg:".$res['errmsg']);
}
}
return $this->redis->get('wechat_access_token');
}
/**
* 微信获取code码
* @param url 前端地址
* @return void
*/
function getcode():viod{
$reques_url = $_POST['url'] ?? 'https://baidu.com';
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.self::APPID.'&redirect_uri='.urlencode($reques_url).'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
header("Location:$url");//跳转
}
/**
* 微信登录
* @param code $code码 客户端传递的code编码
* @return arrary
*/
function wxlogin():array{
$openid = $_POST['openid'] ?? '';
$access_token = self::get_access_token();
//获取用户信息
$url ='https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN';
$userinfo = json_decode($url,self::curl('GET',$url,[]),true);
isset($userinfo['errmsg']) ? self::show(0,'失败:'.$userinfo['errmsg']) : self::show(1,'成功',$userinfo);
}
/**
* 返回
* @param code
* @param msg
* @param data
* @return json|void
*/
function show($code = 1, $msg = '', $data = []):void{
echo json_encode(array('code' => $code, 'msg' => $msg, 'data' => $data));
exit;
}
/**
* Curl操作
* @param string $type 请求类型 'POST' 或 'GET' 大小写都可以
* @param string $url 请求地址 url
* @param array $data 数组 cookie 请求cookie data post请求数据
* @param bool $headerFile 返回头信息 如果页面做了跳转 则可以从返回头信息获得跳转地址,应用场景不多
* @return bool|mixed
*/
function curl($type, $url, $data=[], $headerFile=false) {
$type = strtoupper($type);
$type_list = ['POST', 'GET', 'PUT'];
if(!in_array($type, $type_list)) $type = 'POST';
$ch = curl_init();
// 请求类型
if($type == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
}else if($type == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
}
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_ENCODING, ''); // 这个是解释gzip内容, 解决获取结果乱码 gzip,deflate
// 是否存在请求字段信息
if(!empty($data['data'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data['data']);
}
// 是否存在cookie
if(!empty($data['cookie'])) {
curl_setopt($ch, CURLOPT_COOKIE, $data['cookie']);
}
// 请求头
if(!empty($data['header'])) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $data['header']);
}
// 设置代理
if(!empty($data['proxy'])) {
curl_setopt ($ch, CURLOPT_PROXY, $data['proxy']);
}
// 证书
if(!empty($data['ssl_cert'])) {
curl_setopt($ch,CURLOPT_SSLCERT, $data['ssl_cert']);
}
if(!empty($data['ssl_key'])) {
curl_setopt($ch,CURLOPT_SSLKEY, $data['ssl_key']);
}
// 返回ResponseHeader
if($headerFile) {
curl_setopt($ch, CURLOPT_HEADER, 1);
}
// 设置请求超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
// 发送请求
$result = curl_exec($ch);
if (curl_errno($ch)) return false;
curl_close($ch);
return $result;
}
}
$class = new Wechat();
$access_token = $class->getcode();
?>