|
欢迎一起讨论,使用ci半年,获益良多。希望一起进步,qq 1113288996
weixin.phpPHP复制代码 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Weixin
{
private $appId;
private $appSecret;
function __construct ()
{
$this->appId = trim('');
$this->appSecret = trim('');
}
function redirect_url ($redirect)
{
/*授权页面*/
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appId&redirect_uri=$redirect&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
return $url;
}
/* 通过code换取access_token*/
function access_token ($code)
{
/*获取到的code换取access_token和openid*/
$post_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appId&secret=$this->appSecret&code=$code&grant_type=authorization_code";
$return = $this->postdata($post_url);
$access_token = $return['access_token'];
$openid = $return['openid'];
/*获取微信用户数据*/
$get_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = json_decode(file_get_contents($get_userinfo));
return $userinfo;
}
function eff ($access_token,$openid)
{
/*检测access_token是否正确,errcode=0 为正确*/
$eff_url = "https://api.weixin.qq.com/sns/auth?access_token=$access_token&openid=$openid";
$get_eff =json_decode(file_get_contents($eff_url));
return $get_eff;
}
function get_subscribe ($openid)
{
/*
通过用户openid拉取用户信息中subscribe,0为未关注,1为已关注
*/
$acc_token = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"));
$acc_token = $acc_token->access_token;
// return $acc_token;
$get_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$acc_token}&openid={$openid}&lang=zh_CN";
$subscribe_obj = json_decode(file_get_contents($get_url));
$subscribe = $this->obj_to_arr($subscribe_obj);
return $subscribe['subscribe'];
}
//通过curl方式提交code换取access_token数据
function postdata ($url)
{
header('Content-Type:text/html;charset=utf-8');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL , $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST , FALSE);
curl_setopt($curl, CURLOPT_SSLVERSION , 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER , 1);
$output = curl_exec($curl);
curl_close($curl);
$access = json_decode($output,true);
return $access;
}
/*
这个位置开始是控制器index()传入的微信用户资料处理
*/
function save_session ($data)
{
foreach ($data as $key => $value)
{
$_SESSION[$key] = $value;
}
return $_SESSION;
// print_r($_SESSION);exit();
// unset($_SESSION[0]);
}
function obj_to_arr ($data)
{
// 进行转换成数组 使用 obj_to_arr方式
$data = is_object($data)? get_object_vars($data):$data;
foreach ($data as $key => $value)
{
$arr[$key] = $value;
}
return $arr;
}
}
复制代码
|
评分
-
查看全部评分
|