标签:
/*
* 定向的跳转,为了获取Code
*/
public function getcodeAction(){
$this->view->disable();
$appid = ‘wx94c43716d8a91f3f‘;
$redirect_uri = urlencode(‘http://ford4s.amailive.com/redis/getaccesstoken‘);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1234#wechat_redirect";
header(‘location:‘.$url);
}
public function getaccesstokenAction(){
$this->view->disable();
$appid = ‘wx94c43716d8a91f3f‘;
$appsecret = ‘d4624c36b6795d1d99dcf0547af5443d‘;
/*回调的时候自带的这个参数*/
$code = $_GET[‘code‘];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$data = curl_exec($ch);
curl_close($ch);
/*这里接收到的data数据是Json格式的,我在这转换成数组了*/
$result = json_decode($data,true);
/*取出数组中的access_token这个值*/
$access_token = $result[‘access_token‘];
$expires_in = $result[‘expires_in‘];
/*拿到Openid就知道是哪个用户了,例如:参加活动次数,统计量的统计,没参加一下就写一次,在这里可以写入数据库*/
$openid = $result[‘openid‘];
echo $openid;
}
http://ford4s.amailive.com/redis/getcode【5】把域名和方法一生成一个二维码测试:{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
public function getcodeAction(){
$this->view->disable();
$appid = ‘wx94c43716d8a91f3f‘;
/*基本授权 方法跳转地址*/
$redirect_uri = urlencode(‘http://ford4s.amailive.com/redis/getuserinfo‘);
/*高级授权 snsapi_userinfo*/
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect";
header(‘location:‘.$url);
}
public function getUserInfoAction(){
$this->view->disable();
$appid = ‘wx94c43716d8a91f3f‘;
$appsecret = ‘d4624c36b6795d1d99dcf0547af5443d‘;
/*回调的时候自带的这个参数*/
$code = $_GET[‘code‘];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data,true);
/*取出数组中的access_token这个值*/
$access_token = $result[‘access_token‘];
$openid = $result[‘openid‘];
$URL2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$info = curl_exec($ch);
curl_close($ch);
var_dump($info);
}
/* AppID和AppSecret调用本接口来获取access_token */
public function getaccesstokenAction(){
$this->view->disable();
$appid = ‘wx94c43716d8a91f3f‘;
$appsecret = ‘d4624c36b6795d1d99dcf0547af5443d‘;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$data = curl_exec($ch);
curl_close($ch);
/*这里接收到的data数据是Json格式的,我在这转换成数组了*/
$result = json_decode($data,true);
/*取出数组中的access_token这个值*/
$access_token = $result[‘access_token‘];
return $access_token;
}
标签:
原文地址:http://www.cnblogs.com/tinywan/p/5857990.html