首先在微信测试号中修改一下权限。然后再创建一个tp框架。然后创建一个控制器,然后写下自己的appid,和appsecret.然后写下一个跳转的页面,让他进行跳转。
public function index(){
$appid='wxd5f781eb32b03e2e';
$redirect_uri=urlencode('http://ducaijia.hk01.bdysite.com/vote/index.php/home/index/getcode');
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);
}
然后在创建一个得到code的方法,代码如下
public function getcode(){
$code=$_GET['code'];
$json=$this->access_token($code);
echo $json;
// echo $code;
}
然后在function中进行文件的配置
function https_request($url){
$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_RETURNTRANSFER, 1);
$data=curl_exec($curl);
if(curl_errno($curl)){
return 'ERROR' . curl_error($curl);
}
curl_close($curl);
return $data;
然后再进行获取accesson_token
public function access_token($code){
$appid='wxd5f781eb32b03e2e';
$appsecret='a9c276972646b0fdd010f97a48dcc517';
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
$ret=https_request($url);
return $ret;
}