152 lines
6.5 KiB
PHP
152 lines
6.5 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
// +----------------------------------------------------------------------
|
|
// | Author: liu21st <liu21st@gmail.com>
|
|
// +----------------------------------------------------------------------
|
|
namespace Org\Weixin;
|
|
class WeixinLogin{
|
|
private $login_page_url = "https://open.weixin.qq.com/connect/oauth2/authorize?";//微信登录界面
|
|
private $get_accessToken_url = "https://api.weixin.qq.com/sns/oauth2/access_token?";//获取token的url(此token仅用于授权登录)
|
|
private $get_Token_url = "https://api.weixin.qq.com/cgi-bin/token?";//获取token的url
|
|
private $get_user_info = "https://api.weixin.qq.com/sns/userinfo?";//获取用户信息的url
|
|
private $app_id;//appid
|
|
private $app_key;//AppSecret
|
|
//参数
|
|
public function __construct($config) {
|
|
$this->app_id=$config[app_id];
|
|
$this->app_key=$config[app_key];
|
|
$this->redirect_url=$config[redirect_url];
|
|
}
|
|
//第一步:发起用户同意授权页
|
|
public function send_accept_url($scope,$redirect_uri){//scope方式分别有[snsapi_base|snsapi_userinfo],$redirect_uri为回调地址
|
|
$state = md5(rand(1,1000));
|
|
$query =array(
|
|
'appid' => $this->app_id,
|
|
'redirect_uri' =>$redirect_uri,
|
|
'response_type' => 'code',
|
|
'scope' => $scope,
|
|
'state' => $state,
|
|
);
|
|
$url= $this->login_page_url.http_build_query($query).'#wechat_redirect';
|
|
header("Location:$url");
|
|
}
|
|
//第二步 回调地址,接受微信官方跳转过来的url参数,通过获取的code再去换access_token
|
|
public function code_get_token($get){
|
|
$code = $get['code'];
|
|
$state = $get['state'];
|
|
$query =array(
|
|
'grant_type' => 'authorization_code',
|
|
'code' => $code,
|
|
'secret' => $this->app_key,
|
|
'appid' => $this->app_id,
|
|
);
|
|
$url_request = $this->get_accessToken_url.http_build_query($query);
|
|
//抓取url data并转成json
|
|
$data= file_get_contents($url_request);
|
|
return json_decode($data);
|
|
}
|
|
//第三步
|
|
public function get_userinfo($get){
|
|
$array = array(
|
|
'access_token' => $get->access_token,
|
|
'openid' => $get->openid,
|
|
);
|
|
$url_request = $this->get_user_info.http_build_query($array);
|
|
//抓取url data并转成json
|
|
$data= file_get_contents($url_request);
|
|
return json_decode($data);
|
|
}
|
|
//curl方式 提交数据
|
|
public function curl_post($url,$vars,$second=120,$aHeader=array()){
|
|
$vars=json_encode($vars);
|
|
$ch = curl_init();
|
|
//超时时间
|
|
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
|
|
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
|
|
//这里设置代理,如果有的话
|
|
curl_setopt($ch,CURLOPT_URL,$url);
|
|
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
|
|
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
|
|
if( count($aHeader) >= 1 ){
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
|
|
}
|
|
curl_setopt($ch,CURLOPT_POST, 1);
|
|
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
|
|
$data = curl_exec($ch);
|
|
return $data;
|
|
if($data){
|
|
curl_close($ch);
|
|
}else {
|
|
$error = curl_errno($ch);
|
|
curl_close($ch);
|
|
return $error;
|
|
}
|
|
}
|
|
|
|
//获取 access token
|
|
public function get_access_token(){
|
|
$weixin_access_token=vars("weixin_access_token");
|
|
$weixin_access_token_expires_in=vars("weixin_access_token_expires_in");
|
|
if($weixin_access_token&&$weixin_access_token_expires_in>time()){
|
|
return $weixin_access_token;
|
|
}else{
|
|
$query =array(
|
|
'grant_type' => 'client_credential',
|
|
'appid' => $this->app_id,
|
|
'secret' => $this->app_key,
|
|
);
|
|
$url_request = $this->get_Token_url.http_build_query($query);
|
|
//抓取url data并转成json
|
|
$data= file_get_contents($url_request);
|
|
$data=json_decode($data);
|
|
vars("weixin_access_token",$data->access_token);
|
|
vars("weixin_access_token_expires_in",time()+1800);
|
|
return $data->access_token;
|
|
}
|
|
}
|
|
//获取关注的粉丝数
|
|
public function fans_count(){
|
|
$query =array(
|
|
'access_token' => $this->get_access_token(),
|
|
);
|
|
$url_request = "https://api.weixin.qq.com/datacube/getusercumulate?".http_build_query($query);
|
|
$begin_date=$month_lastday=date('Y-m-d', strtotime(date('Y-m-d')." -1 day"));
|
|
$end_date=date('Y-m-d', strtotime(date('Y-m-d')." -1 day"));
|
|
$postdata=array("begin_date"=>$begin_date,"end_date"=>$end_date);
|
|
$data=$this->curl_post($url_request,$postdata);
|
|
$data= json_decode($data);
|
|
return $data->list[0]->cumulate_user;
|
|
}
|
|
//获取新关注的粉丝数
|
|
public function newfans_count(){
|
|
$query =array(
|
|
'access_token' => $this->get_access_token(),
|
|
);
|
|
$url_request = "https://api.weixin.qq.com/datacube/getusersummary?".http_build_query($query);
|
|
$begin_date=$month_lastday=date('Y-m-d', strtotime(date('Y-m-d')." -2 day"));
|
|
$end_date=date('Y-m-d', strtotime(date('Y-m-d')." -1 day"));
|
|
$postdata=array("begin_date"=>$begin_date,"end_date"=>$end_date);
|
|
$data=$this->curl_post($url_request,$postdata);
|
|
$data= json_decode($data);
|
|
return $data->list[1]->new_user;
|
|
}
|
|
//获取粉丝的详细信息
|
|
public function get_fans_detail(){
|
|
$query =array(
|
|
'access_token' => $this->get_access_token(),
|
|
);
|
|
$url_request = "https://api.weixin.qq.com/datacube/getusersummary?".http_build_query($query);
|
|
$begin_date=$month_lastday=date('Y-m-d', strtotime(date('Y-m-d')." -2 day"));
|
|
$end_date=date('Y-m-d', strtotime(date('Y-m-d')." -1 day"));
|
|
$postdata=array("begin_date"=>$begin_date,"end_date"=>$end_date);
|
|
$data=$this->curl_post($url_request,$postdata);
|
|
$data= json_decode($data);
|
|
return $data->list[1]->new_user;
|
|
}
|
|
}
|